删除NSManagedObject后出错“操作无法完成”

时间:2013-08-19 15:54:23

标签: iphone objective-c core-data

所以我有核心数据& iCloud组合数据库对我来说很好。

有些用户在尝试删除在UITableView中呈现的NSManagedObject后发生了一定的崩溃。在我附加到crashreports的调试日志中,崩溃前有以下错误日志:

2013-05-08 22:38:51.851 MyApp[11819:907] Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)", {
}

不幸的是,有关错误的信息:{}是空的,我自己从未遇到错误,因此我不知道如何重新创建错误。尽管如此,它确实至少发生过21次。 (更糟糕的是,一旦他们重新打开App,一些NSManagedObject处于无效状态,并且我的tableview在加载它时出现问题。但是就在旁边)

实际错误是" SIGABRT" in" tableView:commitEditingStyle:forRowAtIndexPath:",这里是相应的代码:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ICLog(@"Deleting the row %i,%i from the data source", indexPath.row, indexPath.section);
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        //Save the context
        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
    }   
}

PS:从日志中也明确了这一切都是从主线程运行的。

这里有人也遇到过这个问题吗?

更新

幸运的是我在另一个我正在使用的应用程序中遇到了同样的错误,在我可以访问的设备中(到目前为止,我从未在任何设备上看到过这种情况),所以现在我可以调查它发生的时候。这就是我发现的:

a)在设置中关闭icloud后仍然会发生 b)每当我尝试删除或插入任何NSManagedObject时都会发生错误,即使我在启动后立即在Appdelegate中执行此操作,也无需访问其他地方的数据

所以似乎某些事情完全阻止我的数据库被更新。我可以抓住上面的异常,然后App至少没有崩溃。但当然不会保存对数据库的更改。

3 个答案:

答案 0 :(得分:0)

当您尝试保存数据模型的无效状态时,经常会出现[context save:]错误。

由于你之前删除了一个对象,我怀疑这个对象与某个其他仍然存在的对象有关系,但是现在它的关系处于无效状态(对于非可选的关系为nil)

检查删除规则可能有所帮助,也许您删除的对象的某些关系必须是可选的,或者您必须正确传播删除。

答案 1 :(得分:0)

我将在这里做一些假设。

我可以看到您使用CoreData object中的对象删除了fetchedResultsController,但您永远不会将该对象从用作ViewController iVar获取存储的任何内容中取消设置在numberOfRowsInSection中,删除后重新整理您的表格index

请说出原来的[indexPath.row count] is 7,然后继续从indexPath.row = 5删除tableView。然后你继续尝试删除indexPath.row = 6上tableView索引上存在该项的另一个对象,因为你没有重新组织indexs,但它不会存在于CoreData ....鉴于现在最大计数为6,指数值现在为5,最初为6 ....这可能是您的用户所遇到的......

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [yourFetchedDataVariable count];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

    if (editingStyle == UITableViewCellEditingStyleDelete) {
        ICLog(@"Deleting the row %i,%i from the data source", indexPath.row, indexPath.section);
        NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
        [context deleteObject:[self.fetchedResultsController objectAtIndexPath:indexPath]];

        //Save the context
        NSError *error;
        if (![context save:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        }
        // the possible solution
        [yourFetchedDataVariable removeObjectAtIndex:indexPath.row];
        // also now you want to re-order your array index...
        [yourTable reloadData]; // and re order your table indexses aswell
    }
}

答案 2 :(得分:0)

可选关系你确定它是可选的吗?和Xcode在什么意义上可选?我曾经说过这个问题,这是因为“可选”的关系。