gpt4 book ai didi

ios - CoreData更新问题

转载 作者:可可西里 更新时间:2023-11-01 06:16:21 25 4
gpt4 key购买 nike

我的应用程序在后台线程中进行更新,然后保存上下文更改。在主要上下文中,有一个与 NSFetchedResultsController 一起使用的 TableView 。有一段时间更新工作正常,但随后抛出异常。为了检查这一点,我添加了 NSLog(@"%@", [self.controller fetchedObjects]);-controllerDidChangeContent:。这是我得到的:

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

)

此处的故障对象 (0xe972610) 导致崩溃。我在更新期间和保存之前记录了数据。此对象仅在 updatedObjects 中。为什么这个方法可以返回“坏”对象?(此外,在更新过程中,几乎每次更新都会影响此对象。并且只有在某些传递变成“坏”传递之后)。

P.S.:我使用 RestKit 来管理 CoreData。

更新 2:

看起来问题出在 [self.controller fetchedObjects] 枚举中。它是从 - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller 调用的,似乎这些对象甚至在这个方法调用期间也被修改了(我的意思是 CoreData 更新到那时还没有完成)。可能吗?

更新:

当我做某事时出现了异常。像这样:

for (PRBattle *battle in [self.controller fetchedObjects) {
switch (battle.statusScalar) {
case ...
default:
[battle willAccessValueForKey:nil];
NSAssert1(NO, @"Unexpected battle status found: %@", battle);
}
}

异常与 -willAccessValueForKey: 联机。战斗的标量状态是枚举,即绑定(bind)到整数值 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>)"
);
}

最佳答案

崩溃主要原因:“在后台线程中保存容易出错”(http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html//apple_ref/文档/uid/TP40003385-SW7)。当然,我以前读过这个。但是 RestKit 的问题变得更大了。它适用于“contextForCurrentThread”。因此,您可以获得后台线程的唯一上下文(根据同一文档)。但是你不能从主线程中保存它,除非你直接传递它,因为在主线程中这个方法返回另一个上下文。当我看到这个时,我认为 RestKit 为我修复了这个问题(在“正常”保存期间,而不是从主线程),但没有。

第二个是NSFetchedResultsController。当您从 -controllerDidChangeContent: 调用 [self.controller fetchedObjects] 时,您应该准备好,这个数组可以及时更改。我拿了这个数组并进行了枚举,检索了必要的数据。在枚举期间它被改变了(我认为,它是一个 NSMutableArray 返回机智类型转换为 NSArray - 所以这种行为成为可能)。您永远不会在“默认”行为中看到这一点(当您为每个呈现的单元格获取带有 objectForIndexPath: 的对象时 - 如 Apple 示例中所示),但在我的应用程序中,除了包含数据对象外,我还有其他单元格。所以我需要生成自己的存储,而不是直接使用 Controller 的获取结果。通过这种方式,我建议完全避免使用 NSFetchedResultsController。我用简单的 NSManagedObjectDidSaveNotification 观察器替换了它,每次都加载数据。

关于ios - CoreData更新问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13259510/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com