Lets say we have NSOperation A and NSOperation B. B is dependant on A finishing and performing some setup as a result of A.completionBlock
being called and finished. This means that B.addDependency(A)
cannot be used as B cannot be constructed until A has finished. Therefore we opt to use A.waitUntilFinished()
on a separate thread before constructing and starting B.
However, because the completionBlock of A is called on a different thread, the thread that we call A.waitUntilFinished()
unblocks before A.completionBlock
has finished executing and hence on construction of B the necessary prerequisites have not been completed.
Because the apple NSOperation API does not provide any control over where A.completionBlock
is dispatched, what is the usual way to handle this issue?
Edit:
The option I've tried so far is to wrap NSOperation B in an NSBlockOperation C and then call C.addDependency(A)
so that B isn't constructed until A is finished. However this still doesn't solve the problem as asynchronous start must still be called within NSBlockOperation C and the completion block still flies off to another thread.