TMob TMob - 2 years ago 74
iOS Question

CoreData concurrency and releasing objects

I'm currently validating our new CoreData architecture which is being used in a multi-threaded environment. For analyzing I'm using GDCoreDataConcurrencyDebugging which prints a warning, every time a ManagedObject is accessed from the wrong thread / queue (as far as I understood).

Now I'm getting tons of warnings like this:

Invalid concurrent access to managed object calling 'release'

I was able to put a break-point where the warning is generated and the code looks like this:

-(MyObject*) createMyObject {
return (MyObject*)[self insertNewObjectEntityWithName:@"MyObject"];

-(NSManagedObject*) insertNewObjectEntityWithName:(NSString*) entityName {
__block NSManagedObject *managedObject;
[self.managedObjectContext performBlockAndWait:^(void) {
managedObject = [NSEntityDescription insertNewObjectForEntityForName:entityName
return managedObject;

Its breaking in the
-method after the return, which I guess is when the objects are being released. Is there anything special I missed with CoreData-concurrency and object-release?
I've looked around and there isn't anything being mentioned about object-release, only about autoreleasepools which I'm not using.

Avi Avi
Answer Source

You are performing the work in a performBlockAndWait: call, which is correct. However, you proceed to return the object, presumably from a different thread. That is not legal. All managed objects must be accessed from the thread/queue on which they are created, with the exception of the objectID property, which is always valid.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download