關於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.



















發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章