2016-03-31 7 views
2

NSManagedObjectのモデルにchildNodesというプロパティがあります。今度はchildNodesプロパティをオーバーライドし、そのフィルタリングされたバージョンを返したいと思いますが、クラッシュしています。これは大丈夫時々作品Core Data-to-Manyプロパティをオーバーライドし、フィルタ処理されたセットを返します

- (NSOrderedSet *)childNodes { 
    [self willAccessValueForKey:@“childNodes”]; 
    NSMutableOrderedSet *result = [self primitiveChildNodes]; 
    [self didAccessValueForKey:@"childNodes”]; 

    NSArray *filteredResult = [[result array] myCustomArrayFilteringMethod]; // let’s say this returns the first half of the array, as a contrived example 

    return [NSOrderedSet orderedSetWithArray:filteredResults]; 
} 

が、私は次のようにクラッシュを見つけることだ:ここで私は私のNSMOサブクラスに持っているものだTerminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSOrderedSet objectsAtIndexes:]: index 24 in index set beyond bounds [0 .. 19]’と私はなぜわかりません。コールスタックは、コレクションを変異が、私は何が起こっているかわからないので、私はコピーを変異していますに関連すると思わ:

0 CoreFoundation   __exceptionPreprocess + 165 
1 libobjc.A.dylib  objc_exception_throw + 48 
2 CoreFoundation   -[NSOrderedSet objectsAtIndexes:] + 952 
3 Foundation    NSKeyValueWillChangeByOrderedToManyMutation + 568 
4 Foundation    NSKeyValueWillChange + 383 
5 Foundation    -[NSObject(NSKeyValueObserverNotification) willChange:valuesAtIndexes:forKey:] + 557 
6 CoreData    -[NSManagedObject(_NSInternalMethods) _excludeObject:fromPropertyWithKey:andIndex:] + 526 
7 CoreData    -[NSManagedObject(_NSInternalMethods) _maintainInverseRelationship:forProperty:oldDestination:newDestination:] + 254 
8 CoreData    -[NSManagedObject(_NSInternalMethods) _didChangeValue:forRelationship:named:withInverse:] + 567 
9 Foundation    NSKeyValueNotifyObserver + 347 
10 Foundation    NSKeyValueDidChange + 466 
11 Foundation    -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118 
12 CoreData    -[NSManagedObject didChangeValueForKey:] + 135 
13 CoreData    -[NSManagedObject(_NSInternalMethods) _updateFromRefreshSnapshot:includingTransients:] + 758 
14 CoreData    -[NSManagedObjectContext(_NestedContextSupport) _copyChildObject:toParentObject:fromChildContext:] + 567 
15 CoreData    -[NSManagedObjectContext(_NestedContextSupport) _parentProcessSaveRequest:inContext:error:] + 1103 
16 CoreData    __82-[NSManagedObjectContext(_NestedContextSupport) executeRequest:withContext:error:]_block_invoke + 364 
17 CoreData    internalBlockToNSManagedObjectContextPerform + 84 
18 libdispatch.dylib  _dispatch_client_callout + 8 
19 libdispatch.dylib  _dispatch_barrier_sync_f_slow_invoke + 284 
20 libdispatch.dylib  _dispatch_client_callout + 8 
21 libdispatch.dylib  _dispatch_main_queue_callback_4CF + 1738 
22 CoreFoundation   __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9 
23 CoreFoundation   __CFRunLoopRun + 2073 
24 CoreFoundation   CFRunLoopRunSpecific + 488 
25 GraphicsServices  GSEventRunModal + 161 
26 UIKit     UIApplicationMain + 171 
27 My app     main + 111 
28 libdyld.dylib 

任意の提案ですか?私はこれを理解できません。

答えて

0

元の関係のフィルタリングされたコピーを返す別のメソッドを追加します。また、実装のためにコレクションを繰り返し、保持したいアイテムをまったく新しいコレクションに追加します。コピーしたりフィルターをかけたりしないでください。

あなたがフィルタリングしている配列がデータストアによって実際にバックアップされているため、クラッシュするのは、フィルタを使用してフィルタを反復しているコンテンツを変更しているためです。

関連する問題