0

我正在使用 Core Data 存储联系人的某些详细信息,例如RecordIDModificationtimestamp等。

我的应用程序崩溃并显示以下消息;

CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  *** -[_PFBatchFaultingArray objectAtIndex:]: index (68) beyond bounds (57) with userInfo (null)
2012-12-12 13:10:11.585 SalesPro[351:907] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[_PFBatchFaultingArray objectAtIndex:]: index (68) beyond bounds (57)'

这是崩溃日志:

Last Exception Backtrace:
0   CoreFoundation                  0x389c029e __exceptionPreprocess + 158
1   libobjc.A.dylib                 0x3519797a objc_exception_throw + 26
2   CoreFoundation                  0x389c01c0 +[NSException raise:format:] + 100
3   CoreData                        0x36f656ce -[_PFBatchFaultingArray objectAtIndex:] +   142
4   CoreData                        0x3701e5ec +[NSFetchedResultsController(PrivateMethods) _insertIndexForObject:inArray:lowIdx:highIdx:sortDescriptors:] + 224
5   CoreData                        0x3701adf2 -[NSFetchedResultsController(PrivateMethods) _postprocessInsertedObjects:] + 510
6   CoreData                        0x3701ca82 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 2402
7   CoreFoundation                  0x38911032 _CFXNotificationPost + 1422
8   Foundation                      0x3a073d8c -[NSNotificationCenter postNotificationName:object:userInfo:] + 68
9   CoreData                        0x36faa302 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 74
10  CoreData                        0x36fa9862 -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 294
11  CoreData                        0x36f2bc06 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 2694
12  CoreData                        0x36f2b10a _performRunLoopAction + 266
13  CoreFoundation                  0x389956c8 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 16
14  CoreFoundation                  0x389939bc __CFRunLoopDoObservers + 272
15  CoreFoundation                  0x38993d12 __CFRunLoopRun + 738
16  CoreFoundation                  0x38906eb8 CFRunLoopRunSpecific + 352
17  CoreFoundation                  0x38906d44 CFRunLoopRunInMode + 100
18  GraphicsServices                0x346df2e6 GSEventRunModal + 70
19  UIKit                           0x3910e2f4 UIApplicationMain + 1116
20  SalesPro                        0x000a4c0c 0xa3000 + 7180
21  libdyld.dylib                   0x39ac7b1c start + 0

方法没有矛盾numberOfRowsInSection

编辑

更新表格视图的代码

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

    UITableView *tableView = self.table;

    switch(type) {

        case NSFetchedResultsChangeInsert:
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView cellForRowAtIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeDelete:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation: UITableViewRowAnimationFade];
            break;
        }

    [self.table reloadData];
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    // The fetch controller has sent all current change notifications, so tell the table view to process all updates.
    [self.table endUpdates];

    [table reloadData];
}
4

2 回答 2

2

没有更多细节,很难说发生了什么,但你应该实施

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView beginUpdates];
}

该方法为更改设置表。

最后,你应该打电话

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];
}

通知更改完成。

reloadData如果您正确实现了方法,则方法不是必需的- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath

此外,正如@Mundi 建议的那样,您还应该调用- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo来设置对您的部分的更改。

我真的建议阅读 Apple 在NSFetchedResultsControllerDelegate Protocol Reference提供的模板。

此外,您可以查看iOS 5 上的 Core Data 教程:如何使用 NSFetchedResultsController

希望有帮助。

于 2012-12-12T21:29:55.877 回答
0

确保您还实现了处理更全面的部分更改的NSFetchedResultsControllerDelegate方法。controller:didChangeSection:atIndex:forChangeType:

于 2012-12-12T17:46:40.667 回答