NSOperation的使用细节 [1]

简介:

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 .

目录
相关文章
|
1月前
|
C++
C++番外篇——对于继承中子类与父类对象同时定义其析构顺序的探究
C++番外篇——对于继承中子类与父类对象同时定义其析构顺序的探究
53 1
|
3月前
|
Java
【多线程面试题十五】、synchronized可以修饰静态方法和静态代码块吗?
这篇文章讨论了Java中的`synchronized`关键字是否可以修饰静态方法和静态代码块,指出`synchronized`可以修饰静态方法,创建一个类全局锁,但不能修饰静态代码块。
|
编译器 调度
FreeRTOS任务的创建(动态方法和静态方法)
FreeRTOS任务的创建(动态方法和静态方法)
798 0
|
Java 编译器
复习篇【类的构造方法与代码块的执行】
复习篇【类的构造方法与代码块的执行】
复习篇【类的构造方法与代码块的执行】
继承情况下链式调用的异常处理
关于abstract class B<T extends B<T>> ,class A extends B<A>的用法?该用法用在链式调用中有继承关系时该如何使用
100 0
|
Java 编译器 C++
“生而有值”—教你使用构造函数 | 带你学《Java面向对象编程》之五
本节结合多组实例从多个方面介绍了重写构造函数的意义以及构造函数与setter函数的异同,指出了一些编写构造函数相关的注意事项。
“生而有值”—教你使用构造函数   | 带你学《Java面向对象编程》之五
匿名内部类方式使用多线程
在开发中,为了方便使用线程,需要随手开线程,最简单的做法就是采用匿名内部类方式使用多线程。   匿名内部类的格式:     new 类名或者接口名() {       重写方法;     }   本质:是该类的子类对象或者该接口的实现类对象。
1060 0