This question already has an answer here:
- NSOperationQueue serial FIFO queue 3 answers
I'm having trouble understanding the way NSOperationQueue
works.
Say I have:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount=1;
[queue addOperationWithBlock:^{
[someObject someSelector];
}];
[queue addOperationWithBlock:^{
[someObject anotherSelector];
}];
The second block is being called even before the first block finishes - the opposite of what I want. I tried using – performSelectorOnMainThread:withObject:waitUntilDone:
instead, but the second block is still being executed first - presumably because the block thread is not being completed on the main thread, and so it is not blocked with waitUntilDone. I added a break point inside my someSelector block, and it is reached after a break point inside the second block.
I don't quite get it. Help me!!
If there are explicit dependencies between the operations, then use
addDependency
:If your operations are doing asynchronous activity, then you should define a custom operation, and only call
completeOperation
(which will post theisFinished
message) when the asynchronous task is done).and
Thus, with the following code, it won't start
operation2
until the asynchronous task initiated inmain
inSomeOperation
object,operation1
, calls itscompleteOperation
method.