关于Operation 的那些事
在iOS平台的并发编程中,NSOperation和NSOperationQueue扮演着非常重要的角色,很多第三方的库也使用NSOperation来实现并发。比如众所周知的AFNetworking这个第三方网络库,就通过自定义NSOperation的方式,将每一个网络请求,封装成为一个Operation,完成各种各样的网络请求和任务管理的需求。
Operation既可以单独使用,也可以和Operation Queue一起使用。
Cocoa operations are an object-oriented way to encapsulate work that you want to perform asynchronously. Operations are designed to be used either in conjunction with an operation queue or by themselves. Because they are Objective-C based, operations are most commonly used in Cocoa-based application in OS X and iOS.
NSOperation是一个抽象类,要实现具体的逻辑功能,必须使用NSOperation的子类来完成。可以自己subclass 一个NSOperation,或者使用系统内置的两个NSOperation的子类来完成相关的功能。
An operation object is an instance of NSOperation class (in the Foundation framework) that you use to encapsulate work you want your application to perform. The NSOperation class itlself is an abstract base class that must be subclassed in order to do any useful work. Despite being abstract, this class does provide a significant amount of infrastructure to minimize the amount of work you have to do in your own subclasses. in addition, the Foundation framework provides two concrete subclasses that you can use as-is with your existing code.
相比GCD,NSOperation的优点有一下几个:
1、可以实现任务之间的相互依赖关系
2、可以检测任务状态的变化
3、可以支持取消操作
4、当任务完成之后,可以通知客户端
5、还可以设置任务的优先级
摘录官方文档描述:
All operation objects support the following key features:
1、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"
2、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".
3、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.
4、Support for prioritizing operations and thereby affecting their relative execution order.
For more information, see "Changing an Operation's Execution Priority".
5、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".
使用Operation有两个意图:提高程序的并发性,同时也让代码更加简单
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 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 asynhronously on one or more separate threads.