Uitableview 正在删除索引处没有对象的NSFetchedResultController行
我正在尝试使用NSFetchedResultController从TableView中删除行,我已经阅读了许多关于如何执行此操作的示例,但我不知道我做错了什么。这件事我需要帮助 这就是我得到的错误:Uitableview 正在删除索引处没有对象的NSFetchedResultController行,uitableview,core-data,nsfetchedresultscontroller,Uitableview,Core Data,Nsfetchedresultscontroller,我正在尝试使用NSFetchedResultController从TableView中删除行,我已经阅读了许多关于如何执行此操作的示例,但我不知道我做错了什么。这件事我需要帮助 这就是我得到的错误: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 1 in section at index 0' 这是我删除的代码: - (void)
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'no object at index 1 in section at index 0'
这是我删除的代码:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Deleting object at row %d", indexPath.row);
// DeleteObject
NSManagedObject *objToDelete = (NSManagedObject*)[self.fetchedResultsController objectAtIndexPath:indexPath];
// Delete
[self.fetchedResultsController.managedObjectContext deleteObject:objToDelete];
NSError *error = nil;
[self.fetchedResultsController.managedObjectContext save:&error];
if(error)
{
NSLog(@"DeleteRow caused : %@", error);
}
}
这是我的控制器
- (NSFetchedResultsController *)fetchedResultsController {
AppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
context = [appdelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Dagbok" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"header" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:40];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"relationship", nil]];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context sectionNameKeyPath:nil
cacheName:@"Root"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
NSArray *matchingdata = [context executeFetchRequest:fetchRequest error:nil];
return _fetchedResultsController;
}
这是我的目标
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = _tabell;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:(VallaDagbokCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray
arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
您似乎已经消除了对获取结果控制器的延迟加载。因此,不清楚FRC getter中的fetch请求究竟何时执行。看起来这是在委托方法删除行之前发生的 尝试通过重新插入原始延迟加载代码段修复FRC getter:
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}