Cocoa NSOutlineView子类中出现崩溃-NSRangeException
我有一个NSOutlineView的子类,它侦听NSManagedObjectContext更改通知并相应地更新outlineView。在我的用户报告的某个点上,我遇到了一个奇怪的崩溃(我自己无法复制)。。。崩溃本身是一个直接的异常:Cocoa NSOutlineView子类中出现崩溃-NSRangeException,cocoa,core-data,nstableview,nsoutlineview,nsindexset,Cocoa,Core Data,Nstableview,Nsoutlineview,Nsindexset,我有一个NSOutlineView的子类,它侦听NSManagedObjectContext更改通知并相应地更新outlineView。在我的用户报告的某个点上,我遇到了一个奇怪的崩溃(我自己无法复制)。。。崩溃本身是一个直接的异常: *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSIndexSet initWithIndexesInRange:]: Range {1844674
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSIndexSet initWithIndexesInRange:]: Range {18446744073709551614, 1} exceeds maximum index value of NSNotFound - 1'
但令人困惑的部分是发生这种情况的代码:
- (void) addedObject: (CommonListData *) item toExistingSection: (CommonListData *) existingSection atIndex: (NSInteger) index {
if (index != NSNotFound) {
[self beginUpdates];
[self insertItemsAtIndexes: [NSIndexSet indexSetWithIndex: index] inParent: existingSection withAnimation:NSTableViewAnimationEffectFade | NSTableViewAnimationSlideDown];
[self endUpdates];
NSInteger scrollPosition = [self rowForItem: item];
if (scrollPosition != NSNotFound && scrollPosition !=0) {
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:scrollPosition - 1]; // crashes here
现在,如果我正在检查NSNotFound和0,为什么indexSetWithIndex:(scrollPosition-1)
仍然给出NSRangeException?
我还可以检查什么以确保scollPosition有效或无效
不确定这是否相关,但只有当我的核心数据堆栈连接到iCloud,并且我获得了NSPersistentStoredImportubiquitoUsContentChangesNotification,并且我使用上下文从ContextDidSaveNotification与通知执行合并更改时,才会发生这种情况
-[NSOutlineView rowForItem:
如果项目不在大纲中,则返回-1(而不是NSNotFound
)。所以,scrollPosition
是-1,scrollPosition-1
是-2+[NSIndexSet indexSetWithIndex:
接受一个(当然)无符号的NSINTEGER,因此-2变成18446744073709551614。我在开始时检查(index!=NSNotFound)
,所以它不应该。。。而且崩溃总是伴随着NSIndexSet indexetwithindex:scrollPosition
,所以不确定它是否重要,可能就是它。不太清楚为什么它会返回-1而不是NSNotFound,但文档中有这样的说明,而且出于遗留原因,他们可能无法在此时更改它。