Cannot update object that was never inserted

6.4k Views Asked by At

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.

6

There are 6 best solutions below

0
On

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.

0
On

What I think it's happening is that you are not getting the right NSManagedObjectContext.

Think about it as a session. So when you update you are not getting the right session and so your object doesn't exist there.

Before doing the second save try to find your object on that NSManagedObjectContext. If you need further help please describe what happens between the creation and the update.

Getting the wrong NSManagedObjectContext can be due to bad code on the AppDelegate or accessing from another thread other than the main thread.

1
On

Your object is loosing the managedObjectContext. Either use

self.managedObjectContext

or refetch the object in

[[FTAppDelegate sharedAppDelegate] managedObjectContext]

and edit the refetched object and then save it.

2
On

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.

2
On

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".

2
On

I have the same error but different and rare scenario, it happens once in almost 100 attempts. Find my problem below:

I have 2 NSManagedObjects in core data model: 1- Lead 2- LeadAttirbute Lead has 1-M relationship with LeadAttribute.

There is a form that inputs for lead and refresh(create new lead) the form after submitting a lead. If i keep on submitting the leads then at a stage, [managedObjectContext save:&error]; starts giving below error:

Domain=NSCocoaErrorDomain Code=134030 "The operation couldn’t be completed. (Cocoa error 134030.)" UserInfo=0x1f251740 {NSAffectedObjectsErrorKey=( " (entity: LeadInfoAttribute; id: 0x1f2eb920 ; data: {\n attributeId = 0;\n lead = nil;\n optional = nil;\n orderId = 0;\n title = nil;\n value = Bjjbjp;\n})" ), NSUnderlyingException=Cannot update object that was never inserted.}

And it keeps on giving the same error until i dont terminate and re-launch the app. I'm not able to update anything in core data model after this error occur, So my questions are:

1- Can we remove the fault state of core data? i.e to capture and delete the object that is creating trouble before making the save call again.

2- What could be the possible reasons for this issue? Since its very rare and can't reproduce this everytime.