4

I know the CoreData is not thread safe, and I need one MOC per thread, and I am using two threads (one to process incoming updates, one to feed the UI) but... Is it ok to just read an object on a completely different thread like:

NSManagedObject *mo = fetch some object
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
      loop over the objects in a relation, fetch some data and cache it....
});

Where DISPATCH_QUEUE_PRIORITY_BACKGROUND could be any one of DEFAULT, LOW, HIGH. I'm getting some strange behaviour and perhaps I should not be doing any background work, even if its read-only, with my managed object.

If I should not be doing this, any suggestions on how to do it? Create another temporary MOC just for this dispatch_async block?

Jason Leach
  • 3,889
  • 7
  • 37
  • 54
  • 1
    You should create the managedObjectContext inside the thread and then fetch contents from it. By the way, you can use NSPrivateQueueConcurrencyType to fetch in background, why do you want to use your own queue. – Sandeep May 12 '15 at 05:19
  • possible duplicate of [Core Data & Threads](http://stackoverflow.com/questions/14662120/core-data-threads) – Volker May 12 '15 at 06:34
  • I didn't realize I could use the private queue for any of the background thread(s); what I'll do is re-fetch my object on the MOC that uses that before I dispatch the block. Thanks. – Jason Leach May 12 '15 at 14:45

0 Answers0