NSOperation - Forcing an operation to wait others dynamically

后端 未结 7 1836
情话喂你
情话喂你 2020-12-12 21:59

I am trying to implement an operation queue and I have the following scenario:

NSOperation A
NSOperation B
NSOperation C
NSOperation D
NSOperationQueue queue         


        
7条回答
  •  [愿得一人]
    2020-12-12 22:11

    Once an NSOperation is in its main method you have to go through with it. There is no paused state, only finished or cancelled.

    I would implement a NSCopying on operation A which would copy the entire state into a new instance. You would have a delegate method or block which is able to communicate that this operation cannot go through because it is missing info from operation B.

    So the process would go such:

    • Create Operation A, set delegate
    • you cannot proceed, delegate method fires
    • the delegate creates a new operation B, creates a copy of operation A, sets the dependency such that A will wait for B's completion
    • then the delegate cancels the original op A

    Inside the delegate you have to make sure to suspend the queue to avoid a race condition. After the above steps you resume the queue. In operation A you would have multiple places where you check for isCancelled to actually not do any more work in main when it has been cancelled.

提交回复
热议问题