关于Operation 的那些事

发布时间:2020-04-06 04:19:23 作者:zhaoguohui000
来源:网络 阅读:447

在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.



















推荐阅读:
  1. WRONGTYPE Operation against a key holding the wron
  2. MacOS无法挂载NFS Operation not permitted错误怎么办

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

because operation either

上一篇:《SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用》

下一篇:Exchange Server 2019 Install ——(一)准备先决条件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》