Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data unresolved error on save

I'm getting an error when saving my core data context, here is the log:

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\"}";
}

I receive this error after removing and deleting an object:

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

I also noted that i also receive the error for the following scenarios(debugging):

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];

At times, i may pass around either the productList or a product object (both of type NSManagedObject) to other controllers, always using assign in the property and never retain. productList is an object which may contain many product objects.

I am able to create new objects(NSManagedObject), but when it comes to deleting them i get the error mentioned above. When running the application in the simulator i keep a close eye on the sqlite file. after attempting to delete an object(code above), i notice the .sqlite file is removed.

I have created a few Core Data iphone applications with no problem but i seem to be having an issue here. i don't believe i am doing anything out of the ordinary but perhaps i am missing a small detail, but i don't know what!

Why is my .sqlite file being deleted and resulting in this error message on save?

How can i find a more useful error message so i can determine the cause of this error?

like image 744
james Avatar asked Aug 18 '11 15:08

james


1 Answers

Found the problem, but i'm not sure why it fixes it:

Before i would delete/remove the object i would delete the image associated with it(stored in the document's directory).

The image by default has no value(nil), but i would attempt to delete it anyway:

    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

if i instead do a nil check before attempting to remove the image like this, i get no error when i delete the managed object itself:

    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

i guess attempting to access the product's picture when it is nil makes it all invalid.

like image 195
james Avatar answered Nov 06 '22 10:11

james