trying to save NSManagedObjectContext not working

user3251270 picture user3251270 · Jul 26, 2014 · Viewed 7.3k times · Source

I have been trying to figure out this problem for 2 days now. I keep getting an error when I try to save.

//self.data is NSManagedObject. kAppDelegate.moc is the managed object context.
self.data = [NSEntityDescription insertNewObjectForEntityForName:@"Data"
                                 inManagedObjectContext:kAppDelegate.moc];

[self.data setValue:[NSNumber numberWithBool:NO] forKey:@"isit"];
[self.data setValue:@"" forKey:@"name"];

NSError *error;
if(![self.data.managedObjectContext save:&error])
{
    NSLog(@"Save did not complete successfully. Error: %@",
    [error localizedDescription]);
}

When I run it though, this appears in the console:

"CoreData: error: Mutating a managed object 0x10935d4c0 (0x10935d420) after it has been removed from its context."

And this:

Save did not complete successfully. Error: (null)

I can't figure out why this is happening, or why the error is "null".

Answer

Tommie C. picture Tommie C. · Jul 6, 2015

Given this error:

2015-07-06 06:15:05.124 xxx[3609:796500] CoreData: error: Mutating a managed object 0x17423d3e0 (0x1740d0450) after it has been removed from its context.

Found:

In my case; a trace of the initialization sequence (using breakpoints and log message class_initial:%B:%H) revealed that I was creating the context twice. My solution was to simply redirect the redundant call to self.managedObjectContext. I may take time at a later point to track down and eliminate the redundant logic.

Initial Results:

  1. d: init(modelName:):1
  2. mext: findInStore(_:):1
  3. mext: findInStore(_:sortDescriptors:predicate:):1
  4. mext: NSManagedObject:1
  5. d:context DataStore:1
  6. d:persistentStoreCoordinator :1
  7. d: managedObjectModel:1
  8. d: applicationDocumentsDirectory:1
  9. mext: createInStore(_:):1
  10. mext: NSManagedObject:2
  11. d:context DataStore:2

Final Results

  1. db: init(modelName:databaseName:):1
  2. d: init(modelName:):1
  3. mext: findInStore(_:):1
  4. mext: findInStore(_:sortDescriptors:predicate:):1
  5. mext: NSManagedObject:1
  6. d:managedObjectContext managedObjectContext:1
  7. d:persistentStoreCoordinator :1
  8. d: managedObjectModel:1
  9. d: applicationDocumentsDirectory:1

Recommendation:

For others having this problem, I recommend a close inspection of your Core Data Stack's initialization sequence. A context may be created twice or the managed object might be getting deleted.