Multithreading with Core Data

21 Nov
2009

Multithreading is always tricky. Here are some gotchas i had when working with core data, multithreaded.

- be sure to check out apples documentation on it. but well, thats not enough…

- Always generate a NEW CONTEXT for a thread. no sharing!

- set a MERGE POLICY for all contexts. Core Data uses optimistic locking, and by default simply rejects everything that was changed concurrently (even if there were different parts of the class changed)

NSManagedObjectContext *context = [[[NSManagedObjectContext alloc] init] autorelease];
[context setPersistentStoreCoordinator:[[AppDelegate getAppDelegate] persistentStoreCoordinator]];
[context setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];

Well, sometimes automatic merging is not enough. Then its going to be tricky – you have to react on the NSError from the save of the context. And there can be A LOT information it it. Check out this blog post for an in-deph report.

Maybe I’ll put some more information there while my app is progressing…

Related posts:

  1. Debugging Core Data
  2. Core Data Notes from iPhone Tech Talk
  3. CoreData and mergeChangesFromContextDidSaveNotification
  4. Core Animation Crash: Attempt to create two animations for cell

Comment Form

top

Switch to our mobile site