Purpose NSOperationQueue

I want to use NSOperationQueue to dispatch CoreData operations. However, the behavior of the operation queue is not always the same (for example, it sends using libdispatch in iOS 4.0 / OS 10.6, which uses thread pools), and the queue may not always use the same thread (as required by NSManagedObjectContext ).

Can I get a sequential NSOperationQueue to execute on a single thread? Or do I need to create my own simple queuing mechanism for this?

+7
source share
2 answers

Can I get a serial NSOperationQueue to execute on a single thread? Or do I need to create my own simple queuing mechanism for this?

You do not need to do either one or the other. What Core Data really requires is that you do not have two pieces of code that make a difference to the context of the managed entity. There's even a note about this at the very beginning of Concurrency with basic data :

Note. . You can use threads, queue queues, or send queues for concurrency. For brevity, this article uses the "stream" to refer to any of them.

What is really necessary is that you serialize operations in a given context. This happens naturally if you use a single thread, but NSOperationQueue also serializes its operations if you set maxConcurrentOperationCount to 1, so you don’t have to worry about all operations running on the same thread.

+3
source

Apple decided to bind managed objects to real threads. It is not so safe anymore to access the context in different threads - a context without any objects. You can be safe, but its objects are not

-one
source

All Articles