iPhone:核心数据保存错误

Posted

技术标签:

【中文标题】iPhone:核心数据保存错误【英文标题】:iPhone: Core Data saving error 【发布时间】:2010-09-02 05:15:15 【问题描述】:

尝试将对象保存到我的核心数据模型时出现以下错误:

2010-09-01 22:07:50.569 Score Card[26566:207] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-1262.60.3/UITableView.m:904
2010-09-01 22:07:50.570 Score Card[26566:207] Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted). with userInfo (null)
Detected an attempt to call a symbol in system libraries that is not present on the iPhone:
_Unwind_Resume called from function -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] in image CoreData.
2010-09-01 22:07:50.572 Score Card[26566:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections.  The number of sections contained in the table view after the update (1) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (1 inserted, 0 deleted).'
*** Call stack at first throw:
(
 0   CoreFoundation                      0x02661b99 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x027b140e objc_exception_throw + 47
 2   CoreFoundation                      0x0261a238 +[NSException raise:format:arguments:] + 136
 3   Foundation                          0x000bae37 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 116
 4   UIKit                               0x0033a9a6 -[UITableView(_UITableViewPrivate) _endCellAnimationsWithContext:] + 7806
 5   UIKit                               0x0032a75b -[UITableView insertSections:withRowAnimation:] + 56
 6   Score Card                          0x00003918 -[History controller:didChangeSection:atIndex:forChangeType:] + 133
 7   CoreData                            0x0246db81 -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 2177
 8   Foundation                          0x0002a0e1 _nsnote_callback + 145
 9   CoreFoundation                      0x02639f29 __CFXNotificationPost_old + 745
 10  CoreFoundation                      0x025b926a _CFXNotificationPostNotification + 186
 11  Foundation                          0x0001fc8a -[NSNotificationCenter postNotificationName:object:userInfo:] + 134
 12  CoreData                            0x023a8879 -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 89
 13  CoreData                            0x02418193 -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 259
 14  CoreData                            0x0238b208 -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1352
 15  CoreData                            0x023c4e55 -[NSManagedObjectContext save:] + 149
 16  Score Card                          0x000053ad -[TimeRun saveAction] + 65
 17  Score Card                          0x00004116 -[TimeRun countDown] + 326
 18  Foundation                          0x000561c9 __NSFireTimer + 125
 19  CoreFoundation                      0x02642f73 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
 20  CoreFoundation                      0x026445b4 __CFRunLoopDoTimer + 1364
 21  CoreFoundation                      0x025a0dd9 __CFRunLoopRun + 1817
 22  CoreFoundation                      0x025a0350 CFRunLoopRunSpecific + 208
 23  CoreFoundation                      0x025a0271 CFRunLoopRunInMode + 97
 24  GraphicsServices                    0x02f4000c GSEventRunModal + 217
 25  GraphicsServices                    0x02f400d1 GSEventRun + 115
 26  UIKit                               0x002ceaf2 UIApplicationMain + 1160
 27  Score Card                          0x00002998 main + 102
 28  Score Card                          0x00002929 start + 53
)
terminate called after throwing an instance of 'NSException

我只是不确定如何解决此问题。我没有插入任何部分,所以我在这里不知所措。想法?

编辑:

这与我获取结果的方式有关吗?看来我正在使用部分名称的键,但我不想要部分:

- (NSFetchedResultsController *)fetchedResultsController 

    if (fetchedResultsController != nil) 
        return fetchedResultsController;
    

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Mission" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array.
    NSSortDescriptor *startDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:startDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"title" cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    self.fetchedResultsController.delegate = self;

    // Memory management.
    [aFetchedResultsController release];
    [fetchRequest release];
    [startDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;

//end

【问题讨论】:

如果您提供一些代码,我们可能更容易找出问题所在。 更新了我返回部分的方法。 【参考方案1】:

如果你仍然想要一个节名。下面的代码可以工作:

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
       atIndex:(NSUInteger)sectionIndex
 forChangeType:(NSFetchedResultsChangeType)type
switch(type)
   
    case NSFetchedResultsChangeInsert:
        [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;
    default:
        break; 
    

【讨论】:

我不知道这个委托方法是否存在,谢谢【参考方案2】:

这告诉你你的问题:

无效更新:无效数量 部分。段数 包含在表视图之后 更新(1)必须等于数字 表中包含的部分 更新前查看 (1),加上或 减去插入的节数 或已删除(插入 1 个,删除 0 个)。

更新后,您从“numberOfSectionsInTableView:”返回的部分数量不正确。最有可能的是,更新中的某些内容会更改部分,但您会继续返回旧的部分编号。

【讨论】:

【参考方案3】:

将 sectionNameKeyPath: 设置为 nil 解决了我的问题。

【讨论】:

【参考方案4】:

他们说您删除模拟器中的应用程序,然后“清除所有构建目标”,然后重新构建并再次运行。希望这会有所帮助

我的答案基于:'NSInternalInconsistencyException'

【讨论】:

以上是关于iPhone:核心数据保存错误的主要内容,如果未能解决你的问题,请参考以下文章

iPhone 编程 - 使用核心数据进行后台保存

iPhone - 应用程序启动时保存核心数据会导致多个相同的对象

核心数据对现有项目 iphone 的影响

核心数据导致 iPhone 崩溃

核心数据最大存储 iPhone

Core Data iphone调试指针