问题 保存时核心数据未解决错误


保存核心数据上下文时出错,这是日志:

Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x1937a0 {NSAffectedStoresErrorKey=(
    "<NSSQLCore: 0x156410>"
), NSUnderlyingError=0x181a60 "The operation couldn’t be completed. (Cocoa error 4.)", NSFilePath=/var/mobile/Applications/appid/Documents/appname.sqlite}, {
    NSAffectedStoresErrorKey =     (
        "<NSSQLCore: 0x156410>"
    );
    NSFilePath = "/var/mobile/Applications/appid/Documents/appname.sqlite";
    NSUnderlyingError = "Error Domain=NSCocoaErrorDomain Code=4 \"The operation couldn\U2019t be completed. (Cocoa error 4.)\" UserInfo=0x181a30 {NSUnderlyingError=0x108ab0 \"The operation couldn\U2019t be completed. No such file or directory\"}";
}

删除和删除对象后,我收到此错误:

[productList removeMProductObject:product];
[[delegate managedObjectContext] deleteObject:product];
[delegate saveContext];

我还注意到我也收到以下场景(调试)的错误:

1.
    [productList removeMProductObject:product];
    [delegate saveContext];

2.
    [[delegate managedObjectContext] deleteObject:product];
    [delegate saveContext];

3.
    [[delegate managedObjectContext] deleteObject:product];
    [productList removeMProductObject:product];
    [delegate saveContext];

4.
    [[delegate managedObjectContext] deleteObject:product];
    [delegate saveContext];//error
    [productList removeMProductObject:product];
    [delegate saveContext];

5.
    [productList removeMProductObject:product];
    [delegate saveContext];//error
    [[delegate managedObjectContext] deleteObject:product];
    [delegate saveContext];

有时,我可能会绕过 productList 或者a product 对象(两种类型 NSManagedObject)总是使用其他控制器 assign 在财产,永远不会 retainproductList 是一个可能包含很多的对象 product 对象。

一世 上午 能够创建新对象(NSManagedObject),但是当涉及到删除它时,我得到上面提到的错误。在模拟器中运行应用程序时,我会密切关注sqlite文件。在尝试删除对象(上面的代码)后,我注意到.sqlite文件已被删除。

我已经创建了一些Core Data iphone应用程序没有问题,但我似乎在这里有一个问题。我不相信我正在做任何与众不同的事情,但也许我错过了一个小细节,但我不知道是什么!

为什么我的.sqlite文件被删除并导致保存时出现此错误消息?

如何找到更有用的错误消息,以便我可以确定此错误的原因?


4523
2017-08-18 15:19


起源



答案:


发现问题,但我不确定为什么它修复它:

在我删除/删除对象之前,我将删除与其关联的图像(存储在文档的目录中)。

默认情况下,图像没有值(nil),但我会尝试删除它:

    NSError *deleteError = nil;
      [[NSFileManager defaultManager] removeItemAtPath:[DOCUMENTS_DIRECTORY stringByAppendingPathComponent:product.mPictureName] error:&deleteError];
#ifdef DEBUG_MODE
      if(deleteError) {
        NSLog(@"Error deleting image: %@", [deleteError description]);
      }
      else {
        NSLog(@"Image %@ deleted.", product.mPictureName);
      }
#endif

如果我在尝试删除这样的图像之前做了一个零检查,我删除托管对象本身时没有错误:

    NSString *imageName = product.mPictureName;
    if(imageName) {
      NSError *deleteError = nil;
      [[NSFileManager defaultManager] removeItemAtPath:[DOCUMENTS_DIRECTORY stringByAppendingPathComponent:imageName] error:&deleteError];
#ifdef DEBUG_MODE
      if(deleteError) {
        NSLog(@"Error deleting image: %@", [deleteError description]);
      }
      else {
        NSLog(@"Image %@ deleted.", imageName);
      }
#endif
    }
    ...
    //delete NSManagedObject and save

我想当它是零时试图访问产品的图片使它全部无效。


14
2017-08-18 17:35



非常感谢你。我有完全相同的问题,解决方案是先检查图像。 - Eugene
男人,太棒了。我花了很多钱。 - Beaker
这个: “在尝试删除对象(上面的代码)后,我注意到.sqlite文件被删除了。”听起来你实际上正在删除整个Documents目录 imageName 从那以后就没有了 removeItemAtPath: 还会递归地删除目录并将nil附加到文档路径,实际上什么都不做。 - mvds
发生这种情况是因为您在removeItemAtPath中抛出异常。但由于您位于文档保存操作所拥有的块内,因此会崩溃应用程序。 - João Nunes
太棒了..它救了我的一天.. thnx :-) - Devarshi


答案:


发现问题,但我不确定为什么它修复它:

在我删除/删除对象之前,我将删除与其关联的图像(存储在文档的目录中)。

默认情况下,图像没有值(nil),但我会尝试删除它:

    NSError *deleteError = nil;
      [[NSFileManager defaultManager] removeItemAtPath:[DOCUMENTS_DIRECTORY stringByAppendingPathComponent:product.mPictureName] error:&deleteError];
#ifdef DEBUG_MODE
      if(deleteError) {
        NSLog(@"Error deleting image: %@", [deleteError description]);
      }
      else {
        NSLog(@"Image %@ deleted.", product.mPictureName);
      }
#endif

如果我在尝试删除这样的图像之前做了一个零检查,我删除托管对象本身时没有错误:

    NSString *imageName = product.mPictureName;
    if(imageName) {
      NSError *deleteError = nil;
      [[NSFileManager defaultManager] removeItemAtPath:[DOCUMENTS_DIRECTORY stringByAppendingPathComponent:imageName] error:&deleteError];
#ifdef DEBUG_MODE
      if(deleteError) {
        NSLog(@"Error deleting image: %@", [deleteError description]);
      }
      else {
        NSLog(@"Image %@ deleted.", imageName);
      }
#endif
    }
    ...
    //delete NSManagedObject and save

我想当它是零时试图访问产品的图片使它全部无效。


14
2017-08-18 17:35



非常感谢你。我有完全相同的问题,解决方案是先检查图像。 - Eugene
男人,太棒了。我花了很多钱。 - Beaker
这个: “在尝试删除对象(上面的代码)后,我注意到.sqlite文件被删除了。”听起来你实际上正在删除整个Documents目录 imageName 从那以后就没有了 removeItemAtPath: 还会递归地删除目录并将nil附加到文档路径,实际上什么都不做。 - mvds
发生这种情况是因为您在removeItemAtPath中抛出异常。但由于您位于文档保存操作所拥有的块内,因此会崩溃应用程序。 - João Nunes
太棒了..它救了我的一天.. thnx :-) - Devarshi


我有一个类似的问题,值得报告任何陷入这个问题的糟糕的shmo。在我的情况下,我有一个表视图控制器,通过从数据模型中删除相应的数据来处理行删除。当我使用remove ## ObjectName ## Object方法(由自动生成的NSManagedObject子类存根)时,我会收到类似于您报告的错误。

当我改为使用NSManagedObjectContext:deleteObject时,我没有遇到保存上下文的问题。但是,我观察到一个异常被抛出而没有来自控制台的理由。

事实证明,正在为与新删除的对象相对应的行调用tableView方法canEditRowAtIndexPath :.很明显,在调用此方法的操作系统之前,我的行计数需要更新。但是,我不在乎。我只是检查我的托管对象数组的大小是否可以由indexPath行索引。

   if([managedObjSet.objects count] > indexPath.row) {
        ManagedObject* obj = [managedObjectArray objectAtIndex:indexPath.row];
        return ([obj.anotherManagedObjSet count] ==0);
    }

0
2018-01-07 00:43