Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to block an NSOperation until an NSOperationQueue finishes?

I have a data loading operation that needs to be run off the main thread to avoid potential blocking issues. To do this, I use an NSOperationQueue and NSOperations.

One issue that has come up, however, is that one of the operations exists to spawn additional operations based on incoming information. Trying to solve this also solved some minor issues I had elsewhere, as the solution I hit upon was to give the NSOperation it's own queue for sub-tasks.

The problem is that as soon as 'main' exits, the NSOperation is going to be marked as 'finished', regardless of whether or not the sub-queue is finished processing; how do I override that behavior?

like image 550
RonLugge Avatar asked Nov 03 '22 22:11

RonLugge


1 Answers

You could send a waitUntilAllOperationsAreFinished message to your child queue before you exit from your operations main method. This is easy, but not a good idea as it blocks a whole thread which is rather wasteful.

A better solution would be to use the dependency system. Create another operation that has a dependency on your main operation. After you create your child operations also add them as a dependency to that new 'finishing' operation.

like image 57
Sven Avatar answered Nov 15 '22 05:11

Sven