NSOperation的使用细节 [3]

NSOperation的使用细节 [3]

 

这一节我们来写自定义concurrent的operation,自定义concurrent的operation稍微有点复杂,需要按照某些既定的步骤编写才可以完成线程的操作。

Methods to override for concurrent operations (concurrent operation需要重写的一些方法)

Method

Description

start

(Required) All concurrent operations must override this method and replace the default behavior with their own custom implementation. To execute an operation manually, you call its start method. Therefore, your implementation of this method is the starting point for your operation and is where you set up the thread or other execution environment in which to execute your task. Your implementation must not call super at any time.

main

(Optional) This method is typically used to implement the task associated with the operation object. Although you could perform the task in the start method, implementing the task using this method can result in a cleaner separation of your setup and task code.

isExecuting
isFinished

(Required) Concurrent operations are responsible for setting up their execution environment and reporting the status of that environment to outside clients. Therefore, a concurrent operation must maintain some state information to know when it is executing its task and when it has finished that task. It must then report that state using these methods.

Your implementations of these methods must be safe to call from other threads simultaneously. You must also generate the appropriate KVO notifications for the expected key paths when changing the values reported by these methods.

isConcurrent

(Required) To identify an operation as a concurrent operation, override this method and return YES.

接下来就是写类了,如下所示:

//
//  ConcurrentOperation.h
//  NSOperationExample
//
//  Created by YouXianMing on 15/9/5.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface ConcurrentOperation : NSOperation {

    BOOL  _executing;
    BOOL  _finished;
}

@end
//
//  ConcurrentOperation.m
//  NSOperationExample
//
//  Created by YouXianMing on 15/9/5.
//  Copyright (c) 2015年 YouXianMing. All rights reserved.
//

#import "ConcurrentOperation.h"

@implementation ConcurrentOperation

- (void)main {

    // do tasks
    NSLog(@"%@", self.name);

    // at last, you should run this method.
    [self completeOperation];
}

- (void)completeOperation {

    [self willChangeValueForKey:@"isFinished"];
    [self willChangeValueForKey:@"isExecuting"];
    _executing = NO;
    _finished  = YES;
    [self didChangeValueForKey:@"isExecuting"];
    [self didChangeValueForKey:@"isFinished"];
}

- (void)start {

    // Always check for cancellation before launching the task.
    if ([self isCancelled]) {

        // Must move the operation to the finished state if it is canceled.
        [self willChangeValueForKey:@"isFinished"];
        _finished = YES;
        [self didChangeValueForKey:@"isFinished"];

        return;
    }

    // If the operation is not canceled, begin executing the task.
    [self willChangeValueForKey:@"isExecuting"];
    [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
    _executing = YES;
    [self didChangeValueForKey:@"isExecuting"];
}

- (BOOL)isExecuting {

    return _executing;
}

- (BOOL)isFinished {

    return _finished;
}

- (BOOL)isConcurrent {

    return YES;
}

@end

以下是必须携带的内容:

自定义concurrent的operation可以说是nonconcurrent的operation的复杂版本。

 

完整项目:

https://github.com/YouXianMing/NSOperationExample

时间: 2024-07-29 01:14:17

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

NSOperation的使用细节 [1]

NSOperation的使用细节 [1]   NSOperation 使用起来并没有GCD直观,但它有着非常不错的面向对象接口,还可以取消线程操作,这一点是GCD所没有的,NSOperation本身是抽象类,不能够拿它直接使用. 以下节选自 ConcurrencyProgrammingGuide   其中 NSBlockOperation 与 NSInvocationOperation 是直接继承自 NSOperation 的子类,便于你进行简单的线程操作(为了获取更多的操作条件,我们需要通过继

NSOperation的使用细节 [2]

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

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

之前就很想写这样一个很无聊的技术性文章,我这块只是在技术当中也不算是一个码活,靠的就是细致还有很多与生俱来的审美能力,以及不屈不挠的耐心和沉着. 说不出来很多废话,下面来看我的一些比较粗浅的总结 这是一套关于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