我的模型中有一个带有NSManagedObject属性的childNodes。现在,我想覆盖childNodes属性并返回它的过滤后的版本,但是我一直崩溃。这是我的NSMO子类中的内容:

- (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];
}

有时候,这种方法还可以,但是我发现崩溃类似: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

有什么建议?我不太清楚这一点。

最佳答案

只是不要。添加其他方法,该方法返回原始关系的过滤副本。另外,对于实现,只需迭代集合并将要保留的项目添加到全新集合中,请勿复制和过滤。

崩溃是因为您要过滤的数组实际上是由数据存储支持的,因此在进行过滤时,它更改了要使用过滤器进行迭代的内容,并且搞砸了原始关系。

07-24 09:38
查看更多