Ios CoreData更新问题

Ios CoreData更新问题,ios,core-data,restkit,nsfetchedresultscontroller,Ios,Core Data,Restkit,Nsfetchedresultscontroller,我的应用程序在后台线程中进行更新,然后保存上下文更改。 在主上下文中,有一个表视图可与NSFetchedResultsController一起使用。 在一段时间内,更新可以正常工作,但随后会引发异常。 为了检查这一点,我添加了NSLog(@“%@,[self.controller fetchedObjects])至-控制器IDChangeContent:。 以下是我得到的: "<PRBattle: 0x6d30530> (entity: PRBattle; id: 0x6d319d0

我的应用程序在后台线程中进行更新,然后保存上下文更改。 在主上下文中,有一个表视图可与
NSFetchedResultsController
一起使用。 在一段时间内,更新可以正常工作,但随后会引发异常。 为了检查这一点,我添加了
NSLog(@“%@,[self.controller fetchedObjects])
-控制器IDChangeContent:
。 以下是我得到的:

"<PRBattle: 0x6d30530> (entity: PRBattle; id: 0x6d319d0 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p2> ; data: {\n    battleId = \"-1\";\n    finishedAt = \"2012-11-06 11:37:36 +0000\";\n    opponent = \"0x6d2f730 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p1>\";\n    opponentScore = nil;\n    score = nil;\n    status = 4;\n})",
"<PRBattle: 0x6d306f0> (entity: PRBattle; id: 0x6d319f0 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p1> ; data: {\n    battleId = \"-1\";\n    finishedAt = \"2012-11-06 11:37:36 +0000\";\n    opponent = \"0x6d2ddb0 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p3>\";\n    opponentScore = nil;\n    score = nil;\n    status = 4;\n})",
"<PRBattle: 0x6d30830> (entity: PRBattle; id: 0x6d31650 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p11> ; data: <fault>)",
"<PRBattle: 0x6d306b0> (entity: PRBattle; id: 0x6d319e0 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p5> ; data: {\n    battleId = 325;\n    finishedAt = nil;\n    opponent = \"0x6d2f730 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p1>\";\n    opponentScore = 91;\n    score = 59;\n    status = 3;\n})",
"<PRBattle: 0x6d30730> (entity: PRBattle; id: 0x6d31a00 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p6> ; data: {\n    battleId = 323;\n    finishedAt = nil;\n    opponent = \"0x6d2ddb0 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p3>\";\n    opponentScore = 0;\n    score = 0;\n    status = 3;\n})",
"<PRBattle: 0x6d307b0> (entity: PRBattle; id: 0x6d31630 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p9> ; data: {\n    battleId = 370;\n    finishedAt = \"2012-11-06 14:24:14 +0000\";\n    opponent = \"0x79a8e90 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p2>\";\n    opponentScore = 180;\n    score = 180;\n    status = 4;\n})",
"<PRBattle: 0x6d307f0> (entity: PRBattle; id: 0x6d31640 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p10> ; data: {\n    battleId = 309;\n    finishedAt = \"2012-11-02 01:19:27 +0000\";\n    opponent = \"0x79a8e90 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p2>\";\n    opponentScore = 120;\n    score = 240;\n    status = 4;\n})",
"<PRBattle: 0x6d30770> (entity: PRBattle; id: 0x6d31620 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p7> ; data: {\n    battleId = 315;\n    finishedAt = \"2012-11-02 02:26:24 +0000\";\n    opponent = \"0x79a8e90 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PROpponent/p2>\";\n    opponentScore = 119;\n    score = 179;\n    status = 4;\n})"
异常与
-willAccessValueForKey:
联机。battle的标量状态为enum,即绑定到整数值1..4。我已经提到了switch案例中所有可能的值(上面的
default:
)。最后一个有
break。因此,只有当
battle.statusScalar
返回非枚举值时,此方法才可能实现

PRBattle
中的状态标量实现:

- (PRBattleStatuses)statusScalar
{
    [self willAccessValueForKey:@"statusScalar"];
    PRBattleStatuses result = (PRBattleStatuses)[self.status integerValue];
    [self didAccessValueForKey:@"statusScalar"];
    return result;
}
battle.status
具有验证规则: -最小值:1 -最大值:4 -默认值:无值

最后一件事-调试日志:

objc[4664]: EXCEPTIONS: throwing 0x7d33f80 (object 0xe67d2a0, a _NSCoreDataException)
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97b401 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: catch(id)
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97b401 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: handling exception 0x7d33f60 at 0x97b79f
objc[4664]: EXCEPTIONS: rethrowing current exception
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97b911 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x9ac8b7 sp=0xbfffdc20] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97ee80 sp=0xbfffdc40] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x361d0 sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0xa701d8 sp=0xbfffde10] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: catch(id)
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97b911 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: finishing handler
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97b963 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x9ac8b7 sp=0xbfffdc20] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97ee80 sp=0xbfffdc40] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x361d0 sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0xa701d8 sp=0xbfffde10] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: catch(id)
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97b963 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x9ac8b7 sp=0xbfffdc20] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97ee80 sp=0xbfffdc40] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x361d0 sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x3656f sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0xa701d8 sp=0xbfffde10] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: handling exception 0x7d33f60 at 0xa701f5
2012-11-07 13:37:55.463 TestApp[4664:fb03] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  CoreData could not fulfill a fault for '0x6d31650 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p10>' with userInfo {
    NSAffectedObjectsErrorKey =     (
        "<PRBattle: 0x6d30830> (entity: PRBattle; id: 0x6d31650 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p10> ; data: <fault>)"
    );
}
objc[4664]:异常:抛出0x7d33f80(对象0xe67d2a0,一个NSCoreDataException)
objc[4664]:异常:在帧[ip=0x97b401 sp=0xbfffd9b0]中搜索异常0x7d33f60
objc[4664]:异常:catch(id)
objc[4664]:异常:通过帧[ip=0x97b401 sp=0xbfffd9b0]展开异常0x7d33f60
objc[4664]:异常:在0x97b79f处处理异常0x7d33f60
objc[4664]:异常:重新触发当前异常
objc[4664]:异常:在帧[ip=0x97b911 sp=0xbfffd9b0]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0x9ac8b7 sp=0xbfffdc20]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0x97ee80 sp=0xbfffdc40]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0x361d0 sp=0xbfffdc70]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0xa701d8 sp=0xbfffde10]中搜索异常0x7d33f60
objc[4664]:异常:catch(id)
objc[4664]:异常:通过帧[ip=0x97b911 sp=0xbfffd9b0]展开异常0x7d33f60
objc[4664]:异常:完成处理程序
objc[4664]:异常:在帧[ip=0x97b963 sp=0xbfffd9b0]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0x9ac8b7 sp=0xbfffdc20]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0x97ee80 sp=0xbfffdc40]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0x361d0 sp=0xbfffdc70]中搜索异常0x7d33f60
objc[4664]:异常:在帧[ip=0xa701d8 sp=0xbfffde10]中搜索异常0x7d33f60
objc[4664]:异常:catch(id)
objc[4664]:异常:通过帧[ip=0x97b963 sp=0xbfffd9b0]展开异常0x7d33f60
objc[4664]:异常:通过帧[ip=0x9ac8b7 sp=0xbfffdc20]展开异常0x7d33f60
objc[4664]:异常:通过帧[ip=0x97ee80 sp=0xbfffdc40]展开异常0x7d33f60
objc[4664]:异常:通过帧[ip=0x361d0 sp=0xbfffdc70]展开异常0x7d33f60
objc[4664]:异常:通过帧[ip=0x3656f sp=0xbfffdc70]展开异常0x7d33f60
objc[4664]:异常:通过帧[ip=0xa701d8 sp=0xbfffde10]展开异常0x7d33f60
objc[4664]:异常:在0xa701f5处处理异常0x7d33f60
2012-11-07 13:37:55.463 TestApp[4664:fb03]核心数据:错误:严重的应用程序错误。在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托捕获到异常。CoreData无法使用userInfo实现“0x6d31650”的故障{
NSAffectedObjectsErrorKey=(
“(实体:PRBattle;id:0x6d31650;数据:)”
);
}
崩溃的主要原因:“在后台线程中保存容易出错”(http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html//apple_ref/doc/uid/TP40003385-SW7)。当然,我以前读过这个。但是RestKit的问题变得更大了。它与“contextForCurrentThread”一起工作。因此,您可以获得后台线程的唯一上下文(根据同一文档)。但您不能从主线程保存它,除非您直接传递它,因为在主线程中,该方法返回另一个上下文。当我看到这一点时,我认为RestKit为我解决了这个问题(在“正常”保存期间,不是从主线程),但是没有


第二件事是
NSFetchedResultsController
。当您从
-controllerdChangeContent:
调用
[self.controller fetchedObjects]
时,您应该准备好,可以及时更改此数组。我获取这个数组,并进行枚举,检索必要的数据。在枚举过程中,它被更改了(我认为,它是一个
NSMutableArray
返回到
NSArray
的wity类型转换,因此这种行为成为可能)。在“默认”行为中,您永远不会看到这一点(当您为每个呈现的单元格获取对象时,都使用
objectForIndexPath:
,就像在Apple示例中一样),但在我的应用程序中,除了包含数据对象之外,我还有其他单元格。所以我需要生成自己的存储,而不是直接使用控制器的获取结果。这样,我建议您根本不要使用
NSFetchedResultsController
。我已经用simple
NSManagedObjectDidSaveNotification
observer替换了它,并每次加载数据。

是否为每个线程使用不同的NSManagedObjectContext?查看此链接上的公认答案,此外,为什么您说对象0xe972610导致崩溃?如果你指的是“错误”这个词,它意味着另一件事。错误是当一个对象没有完全加载到内存中时,只有当您尝试访问它时,它才会被核心数据透明地加载。当抛出异常时,核心数据到底输出了什么?LombaX,谢谢您的评论。但我读过所有这些,遵守所有规则。我认为它导致崩溃的原因不是bu故障,而是稍后处理这些对象-请参阅我的更新。汤米,谢谢你
objc[4664]: EXCEPTIONS: throwing 0x7d33f80 (object 0xe67d2a0, a _NSCoreDataException)
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97b401 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: catch(id)
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97b401 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: handling exception 0x7d33f60 at 0x97b79f
objc[4664]: EXCEPTIONS: rethrowing current exception
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97b911 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x9ac8b7 sp=0xbfffdc20] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97ee80 sp=0xbfffdc40] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x361d0 sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0xa701d8 sp=0xbfffde10] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: catch(id)
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97b911 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: finishing handler
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97b963 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x9ac8b7 sp=0xbfffdc20] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x97ee80 sp=0xbfffdc40] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0x361d0 sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: searching through frame [ip=0xa701d8 sp=0xbfffde10] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: catch(id)
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97b963 sp=0xbfffd9b0] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x9ac8b7 sp=0xbfffdc20] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x97ee80 sp=0xbfffdc40] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x361d0 sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0x3656f sp=0xbfffdc70] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: unwinding through frame [ip=0xa701d8 sp=0xbfffde10] for exception 0x7d33f60
objc[4664]: EXCEPTIONS: handling exception 0x7d33f60 at 0xa701f5
2012-11-07 13:37:55.463 TestApp[4664:fb03] CoreData: error: Serious application error.  An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:.  CoreData could not fulfill a fault for '0x6d31650 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p10>' with userInfo {
    NSAffectedObjectsErrorKey =     (
        "<PRBattle: 0x6d30830> (entity: PRBattle; id: 0x6d31650 <x-coredata://882BD521-90CD-4682-B19A-000A4976E471/PRBattle/p10> ; data: <fault>)"
    );
}