网站建设资讯

NEWS

网站建设资讯

关于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.

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享标题:关于Operation的那些事-创新互联
URL地址:http://cdweb.net/article/cespch.html