NSOperationQueue concurrent operations with new threads, starting with operations

I just started using NSOperation / NSOprationQueue, so forgive me for asking this question .: P

At the beginning of my application, I want some set of functions to be executed in the queue, so that when it finishes, another is launched (I set setMaxConcurrentOperationCount to 1, so that only one operation happens at a time), Everything should happen in the background , since this is a kind of upload / download to the information server.

I put the first operation in a queue that calls another method that can call some new threads to perform some other actions.

My question is, Will the queue of operations wait until all methods / threads launched in the first operation are completed before the start of the second operation?

+4
source share
3 answers

There are two types of NSOperation s, simultaneous and non-competitive.

Noncompetitive operations are implemented in their -main method, and when this method returns, the operation is considered completed. If you create a thread inside -main and want the operation to be executed until the thread completes, you must block the execution in -main until the thread is executed (for example, using a semaphore).

Parallel operations have a set of predicates like -isExecuting and -isFinished , and the a -start method starts the operation. This method can simply call some background processing and return immediately, the whole operation is not considered complete until -isFinished reports it.

Now that we have a GCD, it is generally recommended that blocks and send queues be considered as an easier alternative to NSOperation , also see the –addOperationWithBlock: method on NSOperationQueue .

+1
source

If NSOperation performs an asynchronous task, for example, [NSURLConnection sendAsynchronousRequest: .....] than the thread on which the operation is performed does not wait for a response, and it will not wait. As soon as the last statement of the last statement or the last block is executed, the operation will be removed from the queue and the next operation will begin.

0
source

You can use something like this

 NSOperationQueue *queue=[NSOperationQueue new]; [queue setMaxConcurrentOperationCount:1]; NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{ //operation first }]; NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{ //operation second }]; [blockOperation2 addDependency:blockOperation1]; [queue addOperation:blockOperation1]; [queue addOperation:blockOperation2]; 
0
source

All Articles