Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOperationQueue designated thread

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?

like image 774
Era Avatar asked Jul 14 '12 19:07

Era


People also ask

What is the difference between Nsoperationqueue and GCD?

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.

What is Nsoperationqueue?

A queue that regulates the execution of operations.


1 Answers

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.

like image 54
Caleb Avatar answered Sep 19 '22 11:09

Caleb