Reputation: 87
I am pretty new to using Core Data and I am having trouble saving my context. Below is a method that gets a teams schedule from my database which then stores the game in the Schedule Core Data entity. But the problem is that the game object won't save when i call context.save(nil). Note that i call the getTeamSchedule() from another method called login() which you can find below.
func getTeamSchedule()
{
let appDel:AppDelegate = UIApplication.sharedApplication().delegate as!AppDelegate
let context:NSManagedObjectContext = appDel.managedObjectContext!
// check if a teams schedule exists in database
if(doesTeamScheduleExist() == true)
{
return
}
else
{
// download schedule
let dao = DatabaseRequests()
let request = dao.buildURLRequest("appGetSchedule.php", postStringWithValue: "teamId=\(self.id)")
let task = NSURLSession.sharedSession().dataTaskWithRequest(request)
{ data, response, error in
if(error == nil)
{
var err:NSError?
var scheduleData = (NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err)) as! NSArray
let entSchdl = NSEntityDescription.entityForName("Schedule", inManagedObjectContext: context)
let entTeam = NSEntityDescription.entityForName("Team", inManagedObjectContext: context)
// loop through data set
for(var index = 0; index < scheduleData.count; index++)
{
/* I Grab My Data From JSON Here*/
var homeTeam : Team
var awayTeam : Team
// check for home team in DB
if(self.doesTeamExistByString(homeId) == true)
{
// get team
homeTeam = self.getStoredTeamByString(homeId)
}
else
{
// create team
homeTeam = self.createNewTeam(homeId, tName: homeName)
}
//check for away team in DB
if(self.doesTeamExistByString(awayId) == true)
{
// get team
awayTeam = self.getStoredTeamByString(awayId)
}
else
{
awayTeam = self.createNewTeam(awayId, tName: awayName)
}
// create a set to store the home and away team objects
// create a set to store the home and away team object
let teamSet:NSSet = ([awayTeam, homeTeam])
// fetch level object
let lvl = self.getStoredLevelByString(level)
var newGame = Schedule(entity: entSchdl!, insertIntoManagedObjectContext: context)
newGame.id = gameId
newGame.homeTeamID = homeId
newGame.awayTeamID = awayId
newGame.levelID = level
newGame.date = dateAsNSDate!
newGame.toTeam = teamSet
newGame.toLevel = lvl
println(newGame)
At this point, I print the newGame object which shows that all required attributes and relationships are filled so i run context.save() but the newGame Object is not saved
var saveError:NSError?
//save context
context.save(&saveError)
println(saveError)
}
}
else
{
println("error =\(error)")
}
}
task.resume()
}
}
Here is the newGame Object
<Schedule: 0x7f8f8ae89110> (entity: Schedule; id: 0x7f8f8ae89180 <x-coredata:///Schedule/t3FF77C1C-6BFD-41F9-9F7E-04D851B11EAB3> ; data: {
awayTeamID = 13;
date = "2015-06-15 05:00:00 +0000";
homeTeamID = 13;
id = 2;
levelID = 1;
toGameLog = (
);
toLevel = "0xd000000000340004 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Level/p13>";
toTeam = (
"0xd000000000840006 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Team/p33>"
);
})
And Here is the Error I am getting
Optional(Error Domain=NSCocoaErrorDomain Code=1580 "The operation couldn’t be completed. (Cocoa error 1580.)" UserInfo=0x7f8f8ae8cfc0 {NSValidationErrorObject=<Schedule: 0x7f8f8ae89110> (entity: Schedule; id: 0x7f8f8ae89180 <x-coredata:///Schedule/t3FF77C1C-6BFD-41F9-9F7E-04D851B11EAB3> ; data: {
awayTeamID = 13;
date = "2015-06-15 05:00:00 +0000";
homeTeamID = 13;
id = 2;
levelID = 1;
toGameLog = (
);
toLevel = "0xd000000000340004 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Level/p13>";
toTeam = (
"0xd000000000840006 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Team/p33>"
);
}), NSValidationErrorValue=Relationship 'toTeam' on managed object (0x7f8f8ae89110) <Schedule: 0x7f8f8ae89110> (entity: Schedule; id: 0x7f8f8ae89180 <x-coredata:///Schedule/t3FF77C1C-6BFD-41F9-9F7E-04D851B11EAB3> ; data: {
awayTeamID = 13;
date = "2015-06-15 05:00:00 +0000";
homeTeamID = 13;
id = 2;
levelID = 1;
toGameLog = (
);
toLevel = "0xd000000000340004 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Level/p13>";
toTeam = (
"0xd000000000840006 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Team/p33>"
);
}) with objects {(
<Team: 0x7f8f8ae87120> (entity: Team; id: 0xd000000000840006 <x-coredata://1B97E1D1-833F-4978-B1A8-05C4BE90A80A/Team/p33> ; data: {
id = 13;
name = "Ghost Team";
toDefenseLineup = "<relationship fault: 0x7f8f8ae80480 'toDefenseLineup'>";
toPlayer = "<relationship fault: 0x7f8f8ae804c0 'toPlayer'>";
toSchedule = (
"0x7f8f8ae89180 <x-coredata:///Schedule/t3FF77C1C-6BFD-41F9-9F7E-04D851B11EAB3>"
);
})
)}, NSValidationErrorKey=toTeam, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1580.)})
I have tried to look up the error code 1580 but can't find anything, if anybody has any resources or knows what the problem is i would appreciate it.
Also out of curiosity what does this mean
relationship fault: 0x7f8f8ae804c0 'toPlayer';
Upvotes: 2
Views: 332
Reputation: 21536
I think the problem is not your newGame
object, but your homeTeam
and awayTeam
objects. Specifically, these two lines:
var homeTeam = Team(entity: entTeam!, insertIntoManagedObjectContext: context)
var awayTeam = Team(entity: entTeam!, insertIntoManagedObjectContext: context)
create two new objects. In your if
statements you reassign these vars to either newly created or newly fetched objects. In either case, the objects created by the lines above do not have any attributes set. When you save the context, these objects fail the validation and trigger the error.
Change the above two lines to:
var homeTeam : Team
var awayTeam : Team
That will declare the two vars without creating new Team objects.
Upvotes: 1