With NSOperationQueue, how do you add to the background instead of the main one, and how does the control of the number of operations work?

I love NSOperationQueue , but I'm having problems with some parts of it.

In the second question of objc.io, they switch to NSOperationQueue and mention that it has two kinds of queues, the main queue that runs on the main thread and background queues. They mention that you can access the main queue using [NSOperation mainQueue] , and then add to manipulate it.

  • You would usually not want to do this, right? If it works in the main thread, will it block the main thread for other tasks? Won't it work simultaneously with other tasks?

It also mentions that you can add to background queues (which I understand would it be better?) By creating NSOperation instances (potentially potentially).

  • Do I keep a reference to the NSOperationQueue that I create for operations to create additional operations? I assume that there are no single characters for background queues, for example, for mainQueue , so how do I control the addition of tasks to background queues?

It also mentions that you can control the number of operations performed simultaneously with the maxConcurrentOperationCount property.

  • I know that you usually set it to NSOperationQueueDefaultMaxConcurrentOperationCount , but if I set it to a specific number manually, does it correspond to the maximum number of threads that can be started immediately? For example, if the processor on the iPhone can start 4 threads at once, and I set this property to 8, what will happen?
+7
ios concurrency objective-c nsoperation nsoperationqueue
source share
3 answers

You are asking:

You would usually not want to [add operations to mainQueue ], right? If it works in the main thread, will it block the main thread for other tasks? Won't it run simultaneously with other tasks?

Yes, you will never want to add anything slow to the main queue. But this does not mean that you are not using the main queue. For some operations (for example, user interface updates) this is critical.

A typical pattern is to create an operation queue for tasks that you want to run in the background, but if later you need to do something that needs to be done in the main queue (for example, updating the user interface, updating the model, etc.).) , you would do this, for example:

 NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ // do some time consuming stuff in the background // when done, you might update the UI in the main queue [[NSOperationQueue mainQueue] addOperationWithBlock:^{ // update the UI here }]; ]; 

You are asking:

Keep a link to the NSOperationQueue that I create for operations in order to be able to create more operations? I assume that for background queues, as for mainQueue, there is no singleton, so how do I manage adding tasks to background queues?

Yes, if you want to add more operations to the same queue later, yes, you want to keep a link to this queue. You can do this by adding it to the application delegate, some kind of central controller or singleton.

But yes, there is no built-in singleton for background queues (because you can possibly have different queues for different operations, for example, one for network operations, one for image processing, etc.). But you can write your own singleton for each line of each type if you want.

You also ask:

I know that you usually set it to NSOperationQueueDefaultMaxConcurrentOperationCount , but if I set it to a specific number manually, does it correspond to the maximum number of threads that can be started immediately? For example, if the processor on the iPhone can run four threads at once, and I set this property to 8, what happens?

You should set maxConcurrentOperationCount as what you think is appropriate for the type of queue. For a network queue, you usually do not exceed 4, but for other types of queues you can easily get more. I believe that there is a maximum of 64 worker threads (which parallel queues use because they need threads).

If you try to use more, the application will not start your operation until the workflow is available. Apple advises, however, to refrain from using all workflows. Therefore, use a reasonable number that matches your queue function. Honestly, one of the advantages of operation queues over dispatch queues is that you can limit the maximum number of workflows that will be used at any given time to better manage the limited resources of the device.


References

+14
source share

As a rule, you do not want to use the main queue. Any operation there will be performed in the main thread.

When creating an activity queue, create it for a specific purpose. As with all server requests. This way you can control how many concurrent requests are running. Therefore, do not add algorithmic processing operations to this queue because they have a different purpose. Keep a link to the queue so that you can add operations in the future (and suspend / cancel operations).

There is no "normal" maxConcurrentOperationCount for maxConcurrentOperationCount - it should be set based on the target.

If you set the value to 8, then the queue will work up to 8 at a time. This is not the most effective option. Always keep the queue assignment.

0
source share

First of all, you need to keep in mind that you always share the main thread with the background thread. Only operations related to updating the user interface should be performed in the main thread, and the remaining operations should be performed in the background thread. for example, if you are dealing with multiple downloads, then you need to take care of all network operations in the background queue, and you will need to update the user interface in the main queue.

 //eg for updating UI in main thread. [self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:YES]; 

Also, when you use the set maxConcurrentOperationCount property as NSOperationQueueDefaultMaxConcurrentOperationCount, this means that the Queue operation accepts the number of concurrent operations depending on the system environment.

Useful links:

http://mobile.tutsplus.com/tutorials/iphone/nsoperationqueue/

http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/

0
source share

All Articles