I want to use an NSOperationQueue
to dispatch CoreData operations. However, operation queue behavior is not always the same (e.g. it dispatches using libdispatch
on iOS 4.0/OS 10.6 which uses thread pools) and a queue might not always use the same thread (as NSManagedObjectContext
requires).
Can I force a serial NSOperationQueue
to execute on a single thread?
Or do I have to create my own simple queuing mechanism for that?
Grand Central Dispatch is a low-level C API that interacts directly with Unix level of the system. NSOperation is an Objective-C API and that brings some overhead with it. Instances of NSOperation need to be allocated before they can be used and deallocated when they are no longer needed.
A queue that regulates the execution of operations.
Can I force a serial NSOperationQueue to execute on a single thread? Or do I have to create my own simple queuing mechanism for that?
You shouldn't need to do either of those. What Core Data really requires is that you don't have two pieces of code making changes to a managed object context at the same time. There's even a note on this at the very beginning of Concurrency with Core Data:
Note: You can use threads, serial operation queues, or dispatch queues for concurrency. For the sake of conciseness, this article uses “thread” throughout to refer to any of these.
What's really required is that you serialize operations on a given context. That 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 ensuring that all operations take place on the same thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With