Domanda

I have created a singleton class that accesses a datastore and returns an object representing a Core Data entity. This is fine in the normal thread but I am concerned about accessing this singleton method from another class running on a background thread as the background thread will be running with its own copy of the Managed Object Context.

When trying to call the singleton from the background thread do I need to create a fresh instance of the singleton and pass in the background thread's copy of the managed object context or can I safely access the singleton method and allow that to use the shared managed object context that it retrieves internally from the app delegate please?

È stato utile?

Soluzione

Have your data manager class make all the relevant Core Data calls with performBlock: or performBlock:andWait:

That way the calls will always be made in a thread safe manner.

Personally, however, I usually write my data manager so that It is called on the main thread, and it takes care of making any further calls on whatever thread makes sense.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top