11

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.

ajayjapan
  • 349
  • 4
  • 18
  • Looks like at the first time when you are inserting the object, it is not inserting into the database. Try to put some dummy string & then update it & i hope it'll resolve your issue. – iCreative Jan 13 '13 at 05:31
  • 1
    "`_category`" feels like the raw ivar portion of a property. If it is a property, what happens when you use "`self.category`"? – Michael Dautermann Jan 13 '13 at 05:32
  • Are you sure that's the same managed object? The object ID (the `x-coredata` URL) looks like a temporary object ID. The `t` is a giveaway. – Tom Harrington Jan 13 '13 at 23:26
  • 1
    Yes the problem is that even after I save the object it is still a temporary object. – ajayjapan Jan 14 '13 at 03:35
  • Even though it seems that they do the same try using NSManagedObject initWithEntity:insertIntoManagedObjectContext: instead. And then check for the method hasChanges on the NSManagedObjectContext. – Fábio Oliveira Jan 30 '13 at 17:17

6 Answers6

4

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.

Kevin R
  • 8,230
  • 4
  • 41
  • 46
  • This seems most correct, as you must actually use insertObject: in the MOC with the newly created object. – h4xnoodle Nov 05 '13 at 22:30
  • @h4xnoodle: [NSEntityDescription insertNewObjectForEntityForName:... inManagedObjectContext:...] does the insertObject on context, so you dont have to call it manually. – JakubKnejzlik Aug 24 '15 at 13:50
1

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

Trausti Thor
  • 3,722
  • 31
  • 41
  • That doesn't make that much sense. The managedObjectContext is like a session in a database and you can do an insert and an update before a commit that is the corresponding in SQL for a refresh. – Fábio Oliveira Jan 30 '13 at 09:56
  • Not sure what to say, you need to call refreshObject, that will fix this. It is in line with the error – Trausti Thor Jan 30 '13 at 15:01
1

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.

  • Can you give more detail as to why this helps? Or where one might be going wrong in the first place? – tslater Jan 23 '15 at 01:34
1

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.

anam
  • 61
  • 1
  • 5
1

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.

JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
0

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.

Fábio Oliveira
  • 2,346
  • 21
  • 30