NSOperationQueue and concurrent vs non-concurrent

后端 未结 2 1284
执笔经年
执笔经年 2020-12-23 12:53

I want to setup a serialized task queue using NSOperationQueue but I\'m a little confused by the terminology discussed in the documentation.

In the co

相关标签:
2条回答
  • 2020-12-23 13:07

    Do you really need to subclass NSOperation? Why not just use NSInvocationOperation and its addDependency: method?

    See my answer in this SO question.

    0 讨论(0)
  • 2020-12-23 13:13

    NSOperationQueue always executes operations concurrently, while taking dependencies into account.

    A "non-concurrent" operation requires a separate thread in order to execute concurrently. NSOperationQueue is responsible for providing this thread. In other words, a non-concurrent operation depends on NSOperationQueue to make it a concurrent operation.

    A "concurrent" operation is concurrent on its own; it doesn't need NSOperationQueue to create a thread for it. An example would be an operation that uses asynchronous file IO.

    If you want two or more operations to execute serially you need to use dependencies.

    If you want an operation to block the main thread then don't use NSOperationQueue; just run each operation one after the other on the main thread.

    To manually set maximum of concurrent operations, use method on operationQueue setMaxConcurrentOperationCount:

    0 讨论(0)
提交回复
热议问题