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 .

时间: 2024-11-10 00:23:25

NSOperation的使用细节 [1]的相关文章

NSOperation的使用细节 [2]

NSOperation的使用细节 [2]     这一节我们来写自定义nonconcurrent的operation,自定义nonconcurrent的operation很简单,重写main方法,之后处理好cancel事件即可.   在开始写nonconcurrent的operation之前,我们需要先了解几个关于NSOperationQueue的细节.   挂起操作 通常情况下,将操作添加到队列中是会立马执行的(如果没有设置队列的最大并发数目),将suspended设置成YES后会将没有执行的

NSOperation的使用细节 [3]

NSOperation的使用细节 [3]   这一节我们来写自定义concurrent的operation,自定义concurrent的operation稍微有点复杂,需要按照某些既定的步骤编写才可以完成线程的操作. Methods to override for concurrent operations (concurrent operation需要重写的一些方法) Method Description start (Required) All concurrent operations m

关注衬线使用上的细节:不益使用过强的渐变

之前就很想写这样一个很无聊的技术性文章,我这块只是在技术当中也不算是一个码活,靠的就是细致还有很多与生俱来的审美能力,以及不屈不挠的耐心和沉着. 说不出来很多废话,下面来看我的一些比较粗浅的总结 这是一套关于Button的实现,如果对HTML有了解的话,应该知道active和hover伪类,并且在网页体现中,他们也起到了很大的作用,以至于牵扯到现在就是设计上也不仅仅只是一个button样式那么简单,还要考虑acitve和hover的效果,就像下面实现的图稿一样 就这么一看,当然什么都没有,但是做

必须知道的Win8.1细节

1. 应用关闭与Win8"很不同" 将应用拖拽至屏幕下方"松手"已不再是Win8.1的应用关闭手势,因为微软将它改成了"一键后台掌"(即按下后仅仅是将应用切至后台,不再像Win8那样直接关闭了),而要想在Win8.1中真正关闭应用, "诀窍"就在于将窗口拖拽至屏幕下方时先不要松手,而是要多停留几秒.在此过程中,你会发现原本被鼠标拖拽着的缩略图翻转成了图标,而一旦翻转完成,再松手才代表着应用被正式关闭. 图1 拖拽到屏幕下方&q

IOS之UI -- 按钮UIButton的细节

按钮细节 文章概要: 1.内部子控件 2.按钮拉伸问题 2-1.代码拉伸 2-2.无代码拉伸 内部子控件 如果想要改变按钮内部子控件的属性,只能自定义按钮 自定义按钮:调整内部子控件的frame 方式1:实现titleRectForContentRect:和imageRectForContentRect:方法,分别返回titleLabel和imageView的frame 方式2:在layoutSubviews方法中设置 内边距 // 设置按钮内容的内边距(影响到imageView和titleLa

国外流行的PS快速保细节磨皮方法

教程虽然是保细节磨皮,不过精度没有双曲线及中性灰等磨皮那么精细,不过过程要简短很多.大致过程:先用修复画笔简单消除一些明显的瑕疵,然后把肤色部分单独选取出来并模糊处理,再适当降低不透明度,这样就可以把皮肤处理光滑.最后调出原人物图层,用滤镜找出需要的细节,应道到效果图上即可. 原图 <点小图查看大图> 最终效果 1.让我们开始通过纠正照片上的颜色.通过使用Ctrl + J复制一层,执行:滤镜 > 模糊 > 高斯模糊,参数及效果如下图. 相关教程: PS不使用滤镜为美女磨皮美容简单教

Photoshop找回丢失的数码图像暗区细节

我们在拍照片时,往往因为天气.环境.或者操作等原因使照片显得过暗.这时,可以通过后期的处理使照片得到修复,弥补拍摄的不足. 这张图片是在公园里拍的,可以看到图片下半部分基本上是一团黑,除了还能看出是房子以外就什么细节也没有了.不过,下面这个小方法,能帮你把细节通通变回来! 图1 1.在Photoshop中打开过暗的图像,为了同原来的做对比,你可以制作一个图像幅本或者复制一个相同图层.执行 "选择"菜单下的"色彩范围". 图2 2.在色彩范围对话框的"选择&

解决午饭们有关Server-u FTP服务器的细节问题

呵呵,这几天有几个午饭对利用server-u ftp搭建FTP很是感兴趣,他们也在向我咨询有关这方面的问题. 我尽量给他们解决他们所遇到的问题,同时也让他们好好的看看了我以前所发的几篇帖子: 烂泥搭建FTP服务器(一):http://bbs.51cto.com/thread-743275-1.html 烂泥搭建FTP服务器(二): http://bbs.51cto.com/thread-744850-1.html 烂泥搭建FTP服务器(三):http://bbs.51cto.com/thread

strlen-最近在做ACM题 发现些c语音细节问题

问题描述 最近在做ACM题 发现些c语音细节问题 #include<cstdio>#include<cstring>int main(){ char s[100]; scanf(""%s""s); if(-1>=strlen(s)) printf(""truen""); return 0;} 竟然打印出true 求解答!!! 解决方案 在Visual C++ 6.0中,原型为size_t strl