Core Data unresolved error on save

10,213

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.

Share:
10,213

Related videos on Youtube

james
Author by

james

Updated on June 16, 2022

Comments

  • james
    james almost 2 years

    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?

  • Eugene
    Eugene over 12 years
    Thank you SO VERY MUCH. I've had exactly the same problem and the solution was to nil check the images first.
  • Beaker
    Beaker about 12 years
    Man, awesome. I spent forever on this.
  • mvds
    mvds about 11 years
    This: "after attempting to delete an object(code above), i notice the .sqlite file is removed." sounds like you were actually deleting the entire Documents directory if imageName was nil, since removeItemAtPath: also removes directories recursively and appending nil to the documents path effectively does nothing.
  • João Nunes
    João Nunes about 11 years
    this happens because you are throwing an exception in removeItemAtPath. but since you are inside a block owned by your document save operation it crashes the app.
  • Devarshi
    Devarshi over 10 years
    awesome.. it saved my day .. thnx :-)