iosdevnyc
iosdevnyc

Reputation: 1871

Is singleton Thread Safe for Core Data?

I have a singleton class and I want to access it from multiple threads. The class return data from core data. I have two nsmanagedobjectcontext for each thread. Should I pass the nsmanagedobjectcontext to the singleton class from each thread?

Upvotes: 0

Views: 768

Answers (1)

morningstar
morningstar

Reputation: 9142

It's safe to access Core Data from different managed object contexts in different threads. You should pass the managed object context to your singleton somehow. Whether your own code in your singleton class is thread safe is up to you.

Another option would be to use one managed object context for all the threads, but lock the persistent store before accessing any Core Data properties. Multiple managed object contexts is preferable, though.

Upvotes: 2

Related Questions