Do NSOperations and their completionBlocks run concurrently?

前端 未结 3 1944
盖世英雄少女心
盖世英雄少女心 2020-12-11 06:51

I\'ve got a bunch of NSOperations added to a NSOperationQueue. The operation queue has the maxConcurrentOperationCount set to 1, so th

3条回答
  •  清歌不尽
    2020-12-11 07:24

    My findings below no longer seem to be true. I've re-run the tests on iOS 8 and iOS 9 and the completion block of an operation always runs concurrently with the next operation. Currently, I don't see a way to make an operation wait for the previous completion block to finish.


    I just tried this scenario in a sample project. Here is the result:

    If the NSOperationQueue's maxConcurrentOperationCount is set to 1, an NSOperation's completionBlock and the next NSOperation in the queue run simultaneously.

    But, if every NSOperation is linked to its previous operation by calling addDependency:, the execution of an operation waits until the previous operation's completionBlock has finished.

    So, if you want to cancel the next operation in the completionBlock of the current operation and be sure that it is cancelled before it is started, you have to set dependencies between the NSOperations by calling addDependency:

提交回复
热议问题