由于错误代码1580,Swift Coredata不会保存

时间:2015-06-24 03:53:03

标签: ios swift core-data

我是使用Core Data的新手,我在保存上下文时遇到了问题。以下是从我的数据库获取团队计划的方法,然后将该游戏存储在计划核心数据实体中。但问题是当我调用context.save(nil)时游戏对象不会保存。请注意,我从另一个名为login()的方法调用getTeamSchedule(),您可以在下面找到它。

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)

此时,我打印newGame对象,该对象显示所有必需的属性和关系都已填充,因此我运行context.save()但未保存newGame对象

                    var saveError:NSError?

                    //save context
                    context.save(&saveError)
                    println(saveError)
                }
            }
            else
            {
                println("error =\(error)")
            }
        }
        task.resume()
    }
}

这是newGame对象

<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>"
);
})

这是我得到的错误

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.)})

我试图查找错误代码1580,但无法找到任何内容,如果有人有任何资源或知道问题是什么我会很感激。

也出于好奇这是什么意思

relationship fault: 0x7f8f8ae804c0 'toPlayer';

1 个答案:

答案 0 :(得分:1)

我认为问题不是您的newGame对象,而是您的homeTeamawayTeam对象。具体来说,这两行:

    var homeTeam = Team(entity: entTeam!, insertIntoManagedObjectContext: context)
    var awayTeam = Team(entity: entTeam!, insertIntoManagedObjectContext: context)

创建两个新对象。在if语句中,您将这些变量重新分配给新创建的或新获取的对象。在任何一种情况下,上面的行创建的对象都没有设置任何属性。保存上下文时,这些对象将无法通过验证并触发错误。

将以上两行更改为:

    var homeTeam : Team
    var awayTeam : Team

这将声明两个变量而不创建新的Team对象。