Reputation: 329
in my app, I create a instance of NSManagedObjectContext, and two threads can access this instance and they all need to commit the changes. what if the save method is called simultaniously in two threads? what will happen to the properties of all NSManagedObject instances in the context?
Upvotes: 0
Views: 310
Reputation: 4371
NSPersistentStoreCoordinator will handle proper locking for NSManagedObjectContexts on multiple threads or queues but NSManagedObjectContext itself should only ever be used on one thread, the thread it was created on (common mistake is to create a MOC on the main thread and then pass it to another thread - do not do this).
As Tony indicated, you want to either have a MOC for each thread, sharing a persistent store coordinator, or use the new concurrency modes for Core Data introduced in iOS 5, allowing you to create child contexts and/or use the new performBlock and performBlockAndWait methods which guarantee that the context executes on the right thread.
A direct answer to your question is 'who knows'. The behavior will be undefined and you will almost certainly run into random crashes and/or deadlocks.
Upvotes: 1
Reputation: 1005
You are not suppose to do like that. Create two separate NSManagedObjectContext for each thread.CoreData is not thread safe.. read this document before doing multithreading with CoreData
Upvotes: 3