NSOperation的使用细节 [1]

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

以下节选自 ConcurrencyProgrammingGuide

 
其中 NSBlockOperation 与 NSInvocationOperation 是直接继承自 NSOperation 的子类,便于你进行简单的线程操作(为了获取更多的操作条件,我们需要通过继承 NSOperation 来设计更复杂的操作)。
 
 
 
 

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]的更多相关文章

  1. NSOperation的使用细节 [3]

    NSOperation的使用细节 [3] 这一节我们来写自定义concurrent的operation,自定义concurrent的operation稍微有点复杂,需要按照某些既定的步骤编写才可以完成 ...

  2. NSOperation的使用细节 [2]

    NSOperation的使用细节 [2] 这一节我们来写自定义nonconcurrent的operation,自定义nonconcurrent的operation很简单,重写main方法,之后处理好c ...

  3. 认识和使用NSOperation

    原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...

  4. 多线程&NSObject&NSThread&NSOperation&GCD

    1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 2.NSOperation/NS ...

  5. iOS之多线程开发NSThread、NSOperation、GCD

    原文出处: 容芳志的博客   欢迎分享原创到伯乐头条 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Centr ...

  6. NSOperation, NSOperationQueue 原理探析

    通过GNUstep的Foundation来尝试探索下NSOperation,NSOperationQueue 示例程序 写一个简单的程序 - (void)viewDidLoad { [super vi ...

  7. iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD

    原文出处: 容芳志的博客 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Central Dispatch) 这 ...

  8. 用NSOperation写下载队列

    用NSOperation写下载队列 说明 1. 支持缓存机制 2. 图片都是在主线程中加载 3. 文件名用了md5加密 *这东西被人写烂了,但大伙如果对NSOperation不熟悉的话,可以看看本人的 ...

  9. [New learn] NSOperation基本使用

    1.简介 NS(基于OC语言)是对GCD(基于C语言)的封装,让开发者能够更加友好的方便的去使用多线程技术. 2.NSOperation的基本使用 NSOperation是抽象类,所以如果要使用NSO ...

随机推荐

  1. PyCharm2018专业版激活步骤

    激活步骤: 1.更改hosts文件,2 获取注册码,  3 完成注册. 1. 更改host文件 hosts文件的路径 : c:\windows\system32\drivers\etc\hosts 将 ...

  2. 忽略“Signal: SIGSEGV (Segmentation fault)”

    1. touch ~/.gdbinit 2. 在.gdbinit文件中添加“handle SIGSEGV nostop noprint pass”

  3. 术语CDATA,其实可以理解为一种特殊的转移字符

    参考:http://www.w3school.com.cn/xml/xml_cdata.asp 常见于XML文档,所有 XML 文档中的文本均会被解析器解析. 只有 CDATA 区段(Charact ...

  4. Node.js最新Web技术栈(2016年4月)

    Node.js是比较简单的,只有你有前端js基础,那就按照我的办法来吧!一周足矣,虽然这版上了es语法,但依然是可以简单写,也可以难写,参见<全栈工程师之路-Node.js>,里面讲了No ...

  5. Verdi文档路径

    1.echo $VERDI_HOME 2.cd $VERDI_HOME/doc 3.okular VerdiTut.pdf& Verdi主要在以下方面使用 Verdi使用情形:        ...

  6. SpringBoot 初接触之 404

    1. 确认输入项目路径是否正确,如:http://localhost:8080/index,这里需要注意的是端口号的查看 2. 确认注解是否用对 在 Controller 层类上面使用的注解是 @Re ...

  7. Node.js HTTP Server对象及GET、POST请求

    上一博客学习了请求与响应,2次读2次写,但有一个问题就是客户端写入的时候怎么知道请求到达.所以HTTP Server对象出现了.它提供了实现HTTP服务器的基本框架.它可以监听端口的底层套接字和接收请 ...

  8. [转]VS 2010 : 如何开发和部署Outlook 2010插件(Add-in)

    本文转自:https://www.cnblogs.com/chenxizhang/archive/2010/05/08/1730766.html 概述: 这篇文章,我将通过一个简单的例子,给大家分享一 ...

  9. [转]Configure Network Drive Visible for SQL Server During Backup and Restore Using SSMS

    本文转自:https://mytechmantra.com/LearnSQLServer/Configure-Network-Drive-Visible-for-SQL-Server-During-B ...

  10. CALayer及其子类

    前言:这个系列要更新Core Animation的内容,但是CALayer是Core Animation的基础. 一 CALayer是什么? 摘自官网的一句话-Layers Provide the B ...