“Collection was mutated while being enumerated” on executeFetchRequest

OK, I think I’ve solved my problem and I must thank this blog post from Fred McCann’s :

Using Core Data on Multiple Threads

The problem seems to come from the fact that I instantiate my background moc on the main thread instead of the background thread. When Apple tells that each thread needs to have its own moc, you have to take it seriously : each moc must be instantiated in the thread that will be using it !

Moving the following lines…

// We instantiate the background moc

self.backgroundMOC = [[[NSManagedObjectContext alloc] init] autorelease];

[self.backgroundMOC setPersistentStoreCoordinator:[self.managedObjectContext persistentStoreCoordinator]];

…in the _importData method (just before to register the controller as observer for the notification) solves the problem.

Thanks for your help, Peter. And thanks to Fred McCann’s for its valuable blog post !

Leave a Comment