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:^{
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