jAckOdE
jAckOdE

Reputation: 2500

What is good practice for using NSOperation and NSOperationQueue?

I just start using NSOperation and NSOperationQueue. I really want to know what should be better practice for using them.

Should I create a only one queue for whole app, or should I create one queue for each specific type of task? What is the effect if I create so many queues?

Upvotes: 2

Views: 797

Answers (2)

Gordon Dove
Gordon Dove

Reputation: 2577

It depends on what you're using them for. If you're using them for performance, maxing out the CPU, then there is little point going much beyond how many cores you expect to have.

If you're offloading processing just to keep the UI responsive, but you're not particularily concerned about maxing out performance, I'd just use the default background queue for simplicity.

If you're using sequential background queues to trigger actions on external events ( network request completion, for example), then it doesn't do any harm to use a bunch, as they won't lead to a lot of threads trying to run concurrently.

There are many instances where you can simplify the design using an operation queue.

NSOperationQueues are built on top of GCD, and AFAIK, there is no performance impact of having loads of queues, as long as they're not trying to execute concurrently.

Upvotes: 1

Stunner
Stunner

Reputation: 12194

Read up on the Executing Operations section of the Apple Concurrency Guide. It depends of course on your implementation and application but in summary ther can only be a certain amount of NSOperations running concurrently and increasing the number of NSOperationQueues won't serve any benefit to you. So my recommendation would be to opt for 1 or fewer queues if your design can allow it.

Upvotes: 1

Related Questions