NSOperation是一套基于GCD的在Cocoa框架中的实现形式,采用面向对象的形式封装了要异步执行的Operation,Foundation framework定义了以下3种Operation:
. NSInvocationOperation,封装了selector的operation
. NSBlockOperation,封装了block的operation
. NSOperation,一个抽象类,需要自定义实现相关selector
具有以下典型特点:
1.继承自NSObject,支持KVO
2.支持Operation之间依赖关系(依赖关系不能是闭环的)的建立,简言之就是Operation之间执行先后顺序是可自定义的,参考dispatch_group的对多个异步task建立组任务,猜想Operation的这个特征应该是基于dispatch_group实现
3.支持Operation的暂停,恢复和取消
4.支持Operation执行所在Queue优先级和Thread优先级的设置,
两个方法setQueuePriority: setThreadPriority:
5.支持completion block,在Operation执行完后会调用该completion block
以下是英文原文:
Support for the establishment of graph-based dependencies between operation objects. These dependencies prevent a given operation from running until all of the operations on which it depends have finished running. For information about how to configure dependencies, see Configuring Interoperation Dependencies.
Support for an optional completion block, which is executed after the operation’s main task finishes. (OS X v10.6 and later only.) For information about how to set a completion block, see Setting Up a Completion Block.
Support for monitoring changes to the execution state of your operations using KVO notifications. For information about how to observe KVO notifications, see Key-Value Observing Programming Guide.
Support for prioritizing operations and thereby affecting their relative execution order. For more information, see Changing an Operation’s Execution Priority.
Support for canceling semantics that allow you to halt an operation while it is executing. For information about how to cancel operations, see Canceling Operations. For information about how to support cancellation in your own operations, see Responding to Cancellation Events.
Operations are designed to help you improve the level of concurrency in your application. Operations are also a good way to organize and encapsulate your application’s behavior into simple discrete chunks. Instead of running some bit of code on your application’s main thread, you can submit one or more operation objects to a queue and let the corresponding work be performed asynchronously on one or more separate threads.
参考:Apple官方文档