NSOperation的使用细节 [1]
NSOperation的使用细节 [1]

NSOperation 使用起来并没有GCD直观,但它有着非常不错的面向对象接口,还可以取消线程操作,这一点是GCD所没有的,NSOperation本身是抽象类,不能够拿它直接使用。
以下节选自 ConcurrencyProgrammingGuide



All operation objects support the following key features
* 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 (page 29).
* 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 (page 31).
* 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 (page 30).
* 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 (page 37). For information about how to support cancellation in your own operations, see Responding to Cancellation Events (page 23).
Concurrent Versus Non-concurrent Operations
Although you typically execute operations by adding them to an operation queue, doing so is not required. It is also possible to execute an operation object manually by calling its start method, but doing so does not guarantee that the operation runs concurrently with the rest of your code. The isConcurrent method of the NSOperation class tells you whether an operation runs synchronously or asynchronously with respect to the thread in which its start method was called. By default, this method returns NO, which means the operation runs synchronously in the calling thread.
If you want to implement a concurrent operation—that is, one that runs asynchronously with respect to the calling thread—you must write additional code to start the operation asynchronously. For example, you might spawn a separate thread, call an asynchronous system function, or do anything else to ensure that the start method starts the task and returns immediately and, in all likelihood, before the task is finished.
Most developers should never need to implement concurrent operation objects. If you always add your operations to an operation queue, you do not need to implement concurrent operations. When you submit a nonconcurrent operation to an operation queue, the queue itself creates a thread on which to run your operation. Thus, adding a nonconcurrent operation to an operation queue still results in the asynchronous execution of your operation object code. The ability to define concurrent operations is only necessary in cases where you need to execute the operation asynchronously without adding it to an operation queue (如果要定义成concurrent操作,只有在这种情形下才可以:你需要异步调用需要的操作,但是又不会将其添加到操作队列当中).
For information about how to create a concurrent operation, see Configuring Operations for Concurrent Execution (page 25) and NSOperation Class Reference .
NSOperation的使用细节 [1]的更多相关文章
- NSOperation的使用细节 [3]
NSOperation的使用细节 [3] 这一节我们来写自定义concurrent的operation,自定义concurrent的operation稍微有点复杂,需要按照某些既定的步骤编写才可以完成 ...
- NSOperation的使用细节 [2]
NSOperation的使用细节 [2] 这一节我们来写自定义nonconcurrent的operation,自定义nonconcurrent的operation很简单,重写main方法,之后处理好c ...
- 认识和使用NSOperation
原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...
- 多线程&NSObject&NSThread&NSOperation&GCD
1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 2.NSOperation/NS ...
- iOS之多线程开发NSThread、NSOperation、GCD
原文出处: 容芳志的博客 欢迎分享原创到伯乐头条 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Centr ...
- NSOperation, NSOperationQueue 原理探析
通过GNUstep的Foundation来尝试探索下NSOperation,NSOperationQueue 示例程序 写一个简单的程序 - (void)viewDidLoad { [super vi ...
- iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD
原文出处: 容芳志的博客 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Central Dispatch) 这 ...
- 用NSOperation写下载队列
用NSOperation写下载队列 说明 1. 支持缓存机制 2. 图片都是在主线程中加载 3. 文件名用了md5加密 *这东西被人写烂了,但大伙如果对NSOperation不熟悉的话,可以看看本人的 ...
- [New learn] NSOperation基本使用
1.简介 NS(基于OC语言)是对GCD(基于C语言)的封装,让开发者能够更加友好的方便的去使用多线程技术. 2.NSOperation的基本使用 NSOperation是抽象类,所以如果要使用NSO ...
随机推荐
- 编写dimgr脚本学到的知识及技巧
编写dimgr是为了管理手机上的镜像,在此总结下过程中学到的知识及技巧(不讨论具体用法). 参数处理 以往处理脚本参数直接用循环加判断语句,若是脚本只有简单参数,这无疑是简便可行的方法.但当需要处理复 ...
- test11
-Xms512m-Xmx512m-XX:PermSize=512-XX:MaxPermSize=512
- Pipenv——最好用的python虚拟环境和包管理工具
pipenv 是Kenneth Reitz大神的作品,能够有效管理Python多个环境,各种包.过去我们一般用virtualenv搭建虚拟环境,管理python版本,但是跨平台的使用不太一致,且有时候 ...
- [转]RDL Report in Visual Studio New page per Record
本文转自:https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f58cd5cf-4296-40f0-b3c8-7e4e15d73762/r ...
- office中把标题之后的空格去掉
调整列表缩进--编号之后不特别标注可以把标题之后的空格去掉
- Abp中SwaggerUI的多个接口文档配置说明
对外提供的接口在实际生成过程中,可能是需要一个接口版本的,比如说v1,manage.效果如下: 在swagger中怎么实现呢? 1. 添加SwaggerVersionHelper.cs pub ...
- favi.icon是什么?
各大网站都有属于自己的图标 概念解释: ico是Icon file的缩写,是Windows的图标文件格式的一种 1.制作ico格式的图片一个上传到服务器的根目录图片保存为favicon.ico 2.制 ...
- C# 数组中的 indexOf 方法
var array=['REG','2018','2018']; array.indexOf(‘REG’) // 0 array.indexOf(‘R’) // -1 array.indexOf(’2 ...
- 第三节:我的第一个Java程序
一.编写我的第一个Java程序流程: 1.新建一个文本文档:在电脑任意位置“右击”----->选择“新建”----->选择“文本文档”: 2.修改文档名与后缀名:“右击”新建的文本文档-- ...
- javaweb中如何给自己的网站更改ico图标
我们在查看网页的时候很多网站都有自己的小图标,系统读取这个标志的时候先从你的项目的根目录下读看有没有favicon.ico文件,如果有直接显示这个图标,如果没有,则会去webapps/root/下找这 ...