【发布时间】:2012-10-16 06:41:32
【问题描述】:
1) 当我们在NSoperationQueue 中添加NSOperation 或NSInvocationOperation 时会发生什么?
2)NSoperation和NSInvocationOperation有什么区别?
3) 哪个最好?
【问题讨论】:
1) 当我们在NSoperationQueue 中添加NSOperation 或NSInvocationOperation 时会发生什么?
2)NSoperation和NSInvocationOperation有什么区别?
3) 哪个最好?
【问题讨论】:
1. NSoperation 和 NSInvocationOperation 的区别
NSoperation 对象是单次对象——也就是说,它只执行一次任务,不能用于再次执行。
NSInvocationOperation 类是 NSOperation 的具体子类,它管理指定为调用的单个封装任务的执行。您可以使用此类启动一个操作,该操作包括在指定对象上调用选择器。这个类实现了一个非并发操作。
2.当我们在 NSoperationQueue 中添加 NSOperation 或 NSInvocationOperation 时会发生什么
您通常通过将操作添加到操作队列(NSOperationQueue 类的实例)来执行操作。操作队列直接执行其操作,通过在辅助线程上运行它们,或间接使用 libdispatch 库(也称为 Grand Central Dispatch)
3.哪个最好
我认为最佳这个词可能会根据您的情况而有所不同:)
【讨论】:
Cocoa Is My Girlfriend 有一个good tutorial 关于 NSOperation 和 NSOperationQueue 的使用。本教程使用 NSOperation 在单独的线程中同时下载多个网页。
也检查这个问题NSOperation on the iPhone
【讨论】: