Iphone – Core Data executeFetchRequest throws NSGenericException (Collection was mutated while being enumerated)

core-dataiphone

I'm developing a iPhone app with Core Data. All user data should be synchronized with our servers. For this purpose I created a subclass of NSOperation witch loads new data from our web service and creates corresponding managed objects. To maintain the relationships between them, every object is transmitted with a remoteID (which is the primary key of the relational server DB).

Let's say there are two managed objects: Department <–>> Employee. The synchronization works as follows:

  1. Load all departments from server. For each department: create a Department object and set its remoteID.

  2. Load all employees from server. For each employee: create Employee object, fetch the related Department (by remoteID) and assign it to the employee.

Fetching a department leads to the following exception:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSCFSet: 0x69c8a10> was mutated while being enumerated.<CFBasicHash 0x69c8a10 [0x2d6d380]>{type = mutable set, count = 1424, 
entries => <A list of all newly created entities>

*** Call stack at first throw:
0 CoreFoundation  0x02d04919 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x02e525de objc_exception_throw + 47
2 CoreFoundation  0x02d043d9 __NSFastEnumerationMutationHandler + 377
3 CoreData        0x026225d0 -[NSManagedObjectContext executeFetchRequest:error:] + 4400
4 myApp           0x00059de4 +[AppFactory departmentWithRemoteID:inManagedObjectContext:] + 259

The exception isn't thrown every time. Moving the code to the main thread resolves the problem. I have no idea what's wrong. I created a new NSManagedObjectContaxt in the synchronization thread and passed all managed objects by its NSManagedObjectID.

Any thoughts?

Best Answer

I had the same problem... It was solved because I was using the managedObjectContext that was created on the main thread on a background thread. The solution was to create a different ManagedObjectContext on the background thread, and use the regular persistentStoreCoordinator... it worked fine after that!

Related Topic