【发布时间】:2013-02-18 11:39:48
【问题描述】:
我的应用程序使用 Core Data 管理的表视图,我在 cimgf.com 的一些好人的帮助下实现了一种重新排序内容的方法。 在此之前,我可以使用以下代码轻松删除对象:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException
// Delete the person object that was swiped
Step *stepToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting (%@)", stepToDelete.name);
[self.managedObjectContext deleteObject:stepToDelete];
[self.managedObjectContext save:nil];
// Delete the (now empty) row on the table
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self performFetch];
[self.tableView endUpdates];
[delegate stepChangedOnMaster:self];
}
}
添加以下代码后,删除对象时,应用崩溃。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath;
{
NSMutableArray *things = [[__fetchedResultsController fetchedObjects] mutableCopy];
// Grab the item we're moving.
NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];
// Remove the object we're moving from the array.
[things removeObject:thing];
// Now re-insert it at the destination.
[things insertObject:thing atIndex:[destinationIndexPath row]];
// All of the objects are now in their correct order. Update each
// object's displayOrder field by iterating through the array.
int i = 0;
for (NSManagedObject *mo in things)
{
NSLog(@"%i - %@", i, [mo valueForKey:@"name"]);
[mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
}
things = nil;
[__managedObjectContext save:nil];
// Save
NSError *error = nil;
if (![__managedObjectContext save:&error])
{
NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
NSLog(@"%@\n\nDetails: %@", msg, details);
}
// re-do the fetch so that the underlying cache of objects will be sorted
// correctly
NSError *errror = nil;
if (![__fetchedResultsController performFetch:&errror])
{
NSLog(@"Unresolved error %@, %@", errror, [errror userInfo]);
abort();
}
[self.tableView reloadData];
}
我真的不明白为什么应用程序崩溃了。有人可以给我一些关于如何解决这个问题的提示吗?谢谢!
这是我得到的错误: * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (0) 必须相等更新前该节中包含的行数 (1),加上或减去从该节插入或删除的行数(0 插入,2 删除),加上或减去移入或移出该节的行数部分(0 移入,0 移出)。 *
【问题讨论】:
-
调试控制台提供了哪些有关崩溃的信息?应该有某种信息。如果您设置了异常断点,那么您可能必须按“继续”一两次才能显示错误消息。
-
用错误更新了问题
标签: ios objective-c core-data ios6 nsfetchedresultscontroller