lwxted
lwxted

Reputation: 18

Can two NSManagedObjectContext on two different threads save (persist to disk) at same time?

Objective C gurus, I have a quick question to ask:

I now have two different threads running in the background, each with an NSManagedObjectContext assigned.

Now, the question is : is it safe for the two NSManagedObjectContext save [persist to disk] using a common / shared NSPersistentStoreCoordinator at the same time?

In other words, can two different background threads persist to disk simultanously?

Thanks a lot.

Upvotes: 0

Views: 176

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

NSPersistentStoreCoordinator is not thread safe but you haven't to worry about it. A NSManagedObjectContext knows how to lock the store when uses it (during a save).

So, it's right to have multiple contexts that share the same store. This is the recommended approach from App Store Documentation:

Create a separate managed object context for each thread and share a single persistent store coordinator.

Hope that helps.

Upvotes: 1

Related Questions