【发布时间】:2015-04-10 23:25:51
【问题描述】:
我有一个 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 时才会发生这种情况。
【问题讨论】:
-
我在一开始就检查
(index != NSNotFound),所以它不应该...而且崩溃总是与NSIndexSet indexSetWithIndex:scrollPosition,所以不确定它是否重要
标签: cocoa core-data nstableview nsoutlineview nsindexset