Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot update object that was never inserted

I create an category object and save it:

    NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];

    _category = (Category *)[NSEntityDescription
                             insertNewObjectForEntityForName:@"Category"
                             inManagedObjectContext:managedObjectContext];

    NSError *error = nil;
    [managedObjectContext save:&error];
    if (error) {
        NSLog(@"error saving: %@",error);
    }

then edit the name of the category object and save again.

    _category.name = _nameTextField.text;

    NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];

    NSError *error = nil;
    [managedObjectContext save:&error];
    if (error) {
        NSLog(@"error saving: %@",error);
    }

and get this error:

 2013-01-12 17:53:11.862 instacat[7000:907] Unresolved error Error Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x2027b300 {NSAffectedObjectsErrorKey=(
"<Category: 0x1ed43cf0> (entity: Category; id: 0x1ed52970 <x-coredata://68E5D7B6-D461-4962-BC07-855349DB3263-7000-00000141BAB4C399/Category/tE8AB2F2E-C14C-4E93-8343-CC245B7726622> ; data: {\n    categoryId = nil;\n    isPrivate = 0;\n    name = techies;\n    users =     (\n    );\n})"
), NSUnderlyingException=Cannot update object that was never inserted.}, {
    NSAffectedObjectsErrorKey =     (
         "<Category: 0x1ed43cf0> (entity: Category; id: 0x1ed52970 <x-coredata://68E5D7B6-D461-4962-BC07-855349DB3263-7000-00000141BAB4C399/Category/tE8AB2F2E-C14C-4E93-8343-CC245B7726622> ; data: {\n    categoryId = nil;\n    isPrivate = 0;\n    name = techies;\n    users =     (\n    );\n})"
    );
     NSUnderlyingException = "Cannot update object that was never inserted.";
}

Thank you for your time and consideration.

I am using the AFIncrementalStore.

like image 445
ajayjapan Avatar asked Jan 13 '13 05:01

ajayjapan


3 Answers

I've just run into this issue and in my case the problem was following:

  • 1) create new managed object in context A
  • 2) save the context A
  • 3) retrieve this object by objectID from context B
  • 4) make changes on managed object and save the context B

Normally it wouldn't be a problem, but in this case the context A is child context and therefore doesn't save to persistent store (just to parent context, which isn't the context B). So when fetch for managed object is done from context B, context doesn't have this object. When changes are made, context tries to save them anyway...and thats when this error occurs. In some cases (as @Trausti Thor mentioned) the refreshObject:mergeChanges: method could help (it passes the data to another context).

In Your case I'll check if:

1) managed object context from [[FTAppDelegate sharedAppDelegate] managedObjectContext] returns always the same context

2) when you save the category, check if it was really saved to persistent store (self.category.objectID.isTemporaryID == NO)

NOTE: The second point is more important, because if you look carefully, your category object still has temporary objectID, that means it's not persisted.

like image 181
JakubKnejzlik Avatar answered Oct 19 '22 19:10

JakubKnejzlik


How about something like this:

self.category.name = self.nameTextField.text;

NSManagedObjectContext *managedObjectContext = [[FTAppDelegate sharedAppDelegate] managedObjectContext];

if(![self.category isInserted])
{
    [managedObjectContext insertObject:self.category];
}

NSError *error = nil;
[managedObjectContext save:&error];
if (error) {
    NSLog(@"error saving: %@",error);
}

Basically check the object is it has been inserted before, if not, insert it and then save the context.

like image 26
Kevin R Avatar answered Oct 19 '22 18:10

Kevin R


When you update an object, you can't use insertNewObjectForEntityForName, you need to first save your object, then call something like

 [self.managedObjectContext refreshObject:_category mergeChanges:YES]

Then use managedObjectContext save again.

This is the difference in direct SQL as "INSERT" and "UPDATE".

like image 24
Trausti Thor Avatar answered Oct 19 '22 19:10

Trausti Thor