objective-c - NSInvocationOperation 和 NSOperationQueue 并发

标签 objective-c multithreading cocoa concurrency grand-central-dispatch

我在 iOS5 中使用 NSInvocationOperationNSOperationQueue 开发。 根据关于调用对象的苹果文档:

The NSInvocationOperation class is a concrete subclass of NSOperation... This class implements a non-concurrent operation.

好的,那么我的 NSInvocationOperation 对象是同步执行的吗? (在这里纠正我)Apple 的文档还提到了操作队列对象:

In iOS, operation queues do not use Grand Central Dispatch to execute operations. They create separate threads for non-concurrent operations and launch concurrent operations from the current thread.

我正在使用 NSInvocationObject,这是一个非并发对象,并将其添加到操作队列中,如下所示:

[operationQueue addOperation:operation];

所以我的问题是:1) 由于操作队列将产生一个单独的线程来执行 NSInvocationObject,正如它在文档中所说的那样,它会异步运行而不是同步运行吗?

[更新 - 回应 Dani 在下方的回答。取自 Apple link .

NSOperation and NSOperationQueue

There are a number of different ways that you can use NSOperation, but the most common is to write a custom subclass and override one method: main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run. NSOperation classes written in this way are known as non-concurrent operations, because the developer is not responsible for spawning threads—multi-threading is all handled by the super class. (Don’t be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don't have to handle the concurrency yourself.)

If you need more control over threading and the run-time environment of your operations, you can make use of concurrent operations. To do this, you subclass NSOperation and override the start method. In the start method, you can spawn threads and setup the environment before calling the main method. You are also required to maintain the state of the NSOperation by setting properties like isExecuting and isFinished. In short, concurrent operations give you a lot more control, but also demand more effort—for most tasks non-concurrent operations suffice.

最佳答案

如果将它添加到操作队列中,它将相对于您的其余代码异步运行,但在操作队列中同步运行。 这很像:

With synchronous objects:

Creating thread
    |
    |\
    | \______
    |        |
    |       Operation A
    |        |
    |       Operation B
    |        |
    |        |

With asynchronous objects:

Creating thread
    |
    |\
    | \___________________
    |                     |
    |\                  Operation A
    | \______             |
    |        |            |
    |       Operation B   |
    |        |            |
    |        |            |

关于objective-c - NSInvocationOperation 和 NSOperationQueue 并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8454137/

相关文章:

iphone - iOS tabBarController 不显示

c - 如何在 c 中创建具有连续 ID 的线程?

java - 跳到单线程 ExecutorService 中的下一个任务?

ios - 对 NSManaged 对象数组进行排序

objective-c - 为什么这是 CTFrame 的过度发布

iphone - 检查应用程序文件夹中是否存在文件

ios - 未调用 UIPageViewController 委托(delegate)方法

java - 如何防止 DynamoDBMapper 中出现 ProvisionedThroughputExceededException?

cocoa - IBOutlet 属性薄弱和 ARC 问题

objective-c - 防止 window 两次打开...