4

私のアプリはバックグラウンドスレッドで更新を行い、コンテキストの変更を保存します。そして、メインコンテキストでは、で動作するテーブルビューがありますNSFetchedResultsController。しばらくの間、更新は正しく機能しますが、例外がスローされます。これを確認するために、に追加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のみ存在します。このメソッドが「悪い」オブジェクトを返すことができるのはなぜですか?(さらに、更新中、このオブジェクトはほとんどすべての更新に影響します。そして、いくつかのパスが「悪い」パスになった後でのみ)。

PS:私はRestKitを使用してCoreDataを管理しています。

UPDATE2:

問題は[self.controller fetchedObjects]列挙にあるようです。これはから呼び出され- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller、このメソッド呼び出し中でもこれらのオブジェクトが変更されているようです(つまり、CoreDataの更新はその時点まで完了していませんでした)。出来ますか?

更新しました:

私がsmthをしたとき、例外がありました。このような:

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

例外はオンライン-willAccessValueForKey:です。戦闘のスカラーステータスは列挙型です。つまり、整数値1..4にバインドされます。スイッチの場合に考えられるすべての値について説明しました(上記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>)"
    );
}
4

1 に答える 1

0

クラッシュの主な理由:「バックグラウンドスレッドでの保存はエラーが発生しやすい」(http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdConcurrency.html//apple_ref/doc/ uid / TP40003385-SW7)。もちろん、私は以前にこれを読んだことがあります。しかし、RestKitでは問題がさらに大きくなりました。「contextForCurrentThread」で動作します。したがって、(同じドキュメントによると)本来あるべきバックグラウンドスレッドの唯一のコンテキストを取得します。ただし、メインスレッドから直接渡す場合を除いて、メインスレッドから保存することはできません。メインスレッドでこのメソッドが別のコンテキストを返すためです。これを見たとき、RestKitがこれを修正したと思いました(メインスレッドからではなく、「通常の」保存中に)が、そうではありません。

2つ目はですNSFetchedResultsController[self.controller fetchedObjects]から電話をかけるときは-controllerDidChangeContent:、この配列を時間内に変更できるように準備しておく必要があります。この配列を取得し、列挙して、必要なデータを取得しました。そして、列挙中に変更されました(これは、NSMutableArray返されるwity型キャストでNSArrayあるため、この動作が可能になります)。これは「デフォルト」の動作では決して表示されません(objectForIndexPath:レンダリングされたセルごとにオブジェクトを取得する場合-Appleの例のように)が、私のアプリでは、データオブジェクトを含む以外に追加のセルがあります。したがって、コントローラーのフェッチされた結果を直接使用するのではなく、独自のストレージを生成する必要があります。そして、このように私はNSFetchedResultsControllerまったく使用しないことをお勧めします。私はそれを単純なNSManagedObjectDidSaveNotificationオブザーバーに置き換え、毎回データをロードしています。

于 2012-11-15T12:01:54.977 に答える