首页 文章

核心数据/ NSFetchedResultsController错误

提问于
浏览
5

我在核心数据/ NSFetchedResultsController上遇到了一些麻烦 . 我不完全确定错误在哪里,因为消息很模糊 .

当获取的结果控制器没有提取任何对象时,我有一个插入多个对象的问题 . 如果我尝试插入几个没有提取的对象,以下代码将崩溃并出现以下错误 . 如果我使用它来插入一个对象,它不会崩溃,如果已经提取了对象,它不会崩溃 .

崩溃发生在save:方法上 . NSArray中的 Headers ,在此示例中,它包含5个字符串 .

严重的应用错误 . 在Core Data更改处理期间捕获到异常:* - [NSCFArray objectAtIndex:]:index(4)超出bounds(1)with userInfo(null)由于未捕获的异常'NSRangeException'终止应用程序,原因:'** - [ NSCFArray objectAtIndex:]:index(4)超出bounds(1)'

NSEnumerator *titleEnumerator = [titles objectEnumerator];
NSString *title;
NSMutableArray *tasks = [NSMutableArray array];
Todo *todo;

while(title = [titleEnumerator nextObject])
{
    todo = (Todo *)[NSEntityDescription insertNewObjectForEntityForName:@"Todo" inManagedObjectContext:managedObjectContext];
    todo.title = title;
    todo.state = [NSNumber numberWithInteger:TodoStateIncomplete];
    todo.priority = [NSNumber numberWithInteger:TodoPriorityNormal];
    todo.timeStamp = [NSDate date];
    todo.dueDate = [NSDate distantFuture];
}

NSError *error;

if(![managedObjectContext save:&error])
{
    NSLog(@"Unresolved error %@ %@", error, [error userInfo]);
    abort();
}

4 回答

  • 5

    以下是Marcus Zarra(Core Data book的作者)的提示:

    Core Data error when deleting row in tableView

    “尝试打破objc_exception_throw并查看抛出异常的方法 . 这应该有助于追踪它”

  • 4

    使用iPhone SDK时,在Core Data上保存的错误通常指向NSFetchedResultsController委托方法中的错误 . 特别是来自Apple的示例代码之一是不正确的,并且经常产生此错误 . 我建议查看您的委托方法并将它们与最新的示例代码进行比较,因为您可能会发现Apple已更新了文档中的示例代码,如果您重新复制其代码,则此错误可能会消失 .

    希望有所帮助 .

  • 0

    我知道它不太可能是答案,但是因为崩溃发生在保存上:而且我确实记得曾经有过一次奇怪的错误......我所犯的错误是这样修复的,我认为它值得一试 .

    所以,说到这一点,尝试改变

    NSError *error;
    

    NSError *error = nil;
    
  • 0

    在我的情况下,这个实现是有帮助的(谢谢你的petert):

    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller{ [self.tableView beginUpdates];  }
    
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
        [self.tableView endUpdates];}
    
    - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
    switch (type) {
        case NSFetchedResultsChangeInsert:
    
            [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
    
        case NSFetchedResultsChangeDelete:
            [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            break;
    
        case NSFetchedResultsChangeUpdate: {
    
            NSString *sectionKeyPath = [controller sectionNameKeyPath];
            if (sectionKeyPath == nil){
                 break;
            }
    
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
            break;
        }
        case NSFetchedResultsChangeMove: {
    
            if (newIndexPath != nil) {
    
                NSUInteger tableSectionCount = [self.tableView numberOfSections];
                NSUInteger frcSectionCount = [[controller sections] count];
                if (frcSectionCount > tableSectionCount)
                    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:[newIndexPath section]] withRowAnimation:UITableViewRowAnimationNone];
                else if (frcSectionCount < tableSectionCount && tableSectionCount > 1)
                    [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationNone];
    
    
                [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
                [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:newIndexPath]
                                      withRowAnimation: UITableViewRowAnimationRight];
    
            }
            else {
                [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[indexPath section]] withRowAnimation:UITableViewRowAnimationFade];
            }
            break;
        }
    }}
    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
    switch (type) {
        case NSFetchedResultsChangeInsert:
            if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1)))
                [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
        case NSFetchedResultsChangeDelete:
            if (!((sectionIndex == 0) && ([self.tableView numberOfSections] == 1) ))
                [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
    
            break;
        case NSFetchedResultsChangeUpdate:
            break;
        case NSFetchedResultsChangeMove:
            break;
    }}
    

相关问题