Reputation: 571
I am working on implementing a manual refresh. That refresh process has 3 steps. Each one needs to be completed before the other (synchronous, serial, etc.). All of them need to be done in background so as not to block the UI.
I've tested the 3 methods individually and they all do what they should (get data from server, update db, etc.) but when I uncomment everything and run, the 2nd is starting before the first is done. And I can't figure out how to fix this. I tried NSInvocationOperation
for the 3 methods and setting dependencies
. didnt work. Tried queue.maxConcurrentOperationCount = 1
. didnt work.
The first method calls other methods and over the course of it, calls other things that create separate thread
s, so is that the issue? When that happens, does it think the operation is done? Is NSOperation
too complicated? Is there a simpler way just to say "do this and dont do this til its done"?
Upvotes: 1
Views: 80
Reputation: 119041
Using NSOperationQueue
and multiple NSOperation
instances, and specifying dependencies between them is the correct approach.
If the operations create other threads and don't wait for them to finish then that would be a problem because the operation would finish and the next operation would start running.
Operations that run a asynchronously should be executed in an NSOperation
subclass. This subclass needs to return YES from isConcurrent
To keep the operation alive after the start/main method returns. The operation is complete when isFinished
is changed to YES (it is observed with KVO so the change notifications must be sent). So, the asynchronous process needs to update and post the appropriate notifications when it has completed. This will terminate the operation and allow the next one to begin.
Upvotes: 3