我有一个 NSOutlineView 的子类,它监听 NSManagedObjectContext 更改通知并相应地更新 outlineView。我在某个时候遇到了一个奇怪的崩溃,我的用户正在报告(我无法自己重现)......崩溃本身就是一个直截了当的 NSRangeException:
*** 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 并且我得到 NSPersistentStoreDidImportUbiquitousContentChangesNotification 并且我使用上下文对通知执行 mergeChangesFromContextDidSaveNotification 时才会发生这种情况。