删除tableView中的行时出现Core Data错误 [英] Core Data error when deleting row in tableView

查看:120
本文介绍了删除tableView中的行时出现Core Data错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewController管理一个分组的tableView。 tableView是从fetchedResultsController填充的。



如果我点击NavigationBar中的编辑按钮,然后选择一行并点击删除按钮,行被删除,所有结束。



但是,如果我滑动显示一行中的删除按钮,并单击删除按钮,应用程序崩溃,并显示以下错误:


2010-01-06 15:25:18.720 Take10 [14415:20b]严重的应用程序错误。在核心数据更改处理期间捕获到异常: - [NSCFArray objectAtIndex:]:index(1)超出界限(1)with userInfo(null)



2010-01-06 15:25:18.721 Take10 [14415:20b]由于未捕获异常NSRangeException终止应用程序,原因:'*** - [NSCFArray objectAtIndex:]:index(1)超出边界(1)'


当然,错误中的索引号会根据我试图删除行的部分中的行数而变化,并且该数字为1



这里是我试图从fetchedResultsController中删除数据的代码。

   - (void)tableView:这个方法会响应这两种情况,所以我不明白为什么它在滑动时崩溃。 (UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if(editingStyle == UITableViewCellEditingStyleDelete){
//删除给定索引路径
NSManagedObjectContext * context = [fetchedResultsController managedObjectContext];
[context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

//保存上下文。
NSError * error = nil;
if(![context save:& error]){
/ *
用代码来替换这个实现,以适当地处理错误。

abort()导致应用程序生成崩溃日志并终止。您不应在运送应用程序中使用此功能,但在开发过程中可能很有用。如果无法从错误中恢复,请显示一个警报面板,指示用户通过按主页按钮退出应用程序。
* /
NSLog(@未解析的错误%@,%@,错误,[错误userInfo]);
abort();
}
}
}

/ p>

感谢



Jk

解决方案

Jeff LaMarche使用NSFetchedResultsController做了一些很好的工作。



尝试在这里使用他的模板:
http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data -application.html



看看是否能解决您的问题。


I have a UITableViewController managing a grouped tableView. The tableView is populated from a fetchedResultsController.

If I click the Edit button in the NavigationBar, then select a row and click the Delete button, the row is deleted and all ends well.

However, if I swipe to reveal the Delete button in a row and click the Delete button, the app crashes with the following error:

2010-01-06 15:25:18.720 Take10[14415:20b] Serious application error. Exception was caught during Core Data change processing: -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)

2010-01-06 15:25:18.721 Take10[14415:20b] Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'

Of course, the index number in the error changes depending on the number of rows in the section where I am attempting to delete a row, and that number is 1 more than the number of remaining rows in the table section after the attempted delete.

Here is the code where I attempt to delete the data from the fetchedResultsController. The same method responds to both scenarios, so I don't understand why it crashes when swiping.

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the managed object for the given index path
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];

    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
  }   
}

Any ideas???

Thanks

Jk

解决方案

Jeff LaMarche has done some good work with NSFetchedResultsController.

Try using his template here: http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html

And see if that solve your issue.

这篇关于删除tableView中的行时出现Core Data错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆