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 |
(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的复杂版本。
完整项目:
NSOperation的使用细节 [3]的更多相关文章
- NSOperation的使用细节 [2]
NSOperation的使用细节 [2] 这一节我们来写自定义nonconcurrent的operation,自定义nonconcurrent的operation很简单,重写main方法,之后处理好c ...
- NSOperation的使用细节 [1]
NSOperation的使用细节 [1] NSOperation 使用起来并没有GCD直观,但它有着非常不错的面向对象接口,还可以取消线程操作,这一点是GCD所没有的,NSOperation本身是抽象 ...
- 认识和使用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 ...
随机推荐
- @Override 注解compiler1.5和compiler1.6不同
说到注解问题,@interface 来定义注解类 这个注解出现是在jdk1.5版本出现. jdk1.5只支持@override对类的继承中方法重写的使用,不支持接口实现中方法重写的使用(这种情况下会报 ...
- 利用ggplot2画出各种漂亮图片详细教程
1.Why use ggplot2 ggplot2是我见过最human friendly的画图软件,这得益于Leland Wilkinson在他的著作<The Grammar of Graphi ...
- 解决启动mongod 时,出现addr already in use错误
启动mongod root@wangyuyu-Vostro-1440:/usr/bin# ./mongod 错误提示: Sat Aug 17 09:02:02 [initandlisten] ER ...
- ARM的体系结构与编程系列博客——ARM的历史与应用范围
前言 最近我感觉自己比较浮躁,重来没有好好地沉下心来做一件事情,而且针对自己在专业水平上仍然还有很多欠缺,于是我想我应该为自己做些什么来证明一下自己真的是潜心研究东西的人,于是我萌生了一个想法,真正地 ...
- Xamarin学习
慧都视频:http://training.evget.com/video/5384 极客学院视频:http://www.jikexueyuan.com/course/364.html
- Spring Boot学习笔记(八)使用jar和war方式打包并在外部Tomcat中部署运行
使用war包的方式发布到外部Tomcat中去 首先修改pom.xml中的配置,使打包方式设置为war包的形式 然后 maven update project 更新下项目 Application入口文件 ...
- vue+element ui 的时间控件选择 年月日时分
前言:工作中用到 vue+element ui 的前端框架,需要选择年月日时分,但element ui官网demo有没有,所以记录一下.转载请注明出处:https://www.cnblogs.com/ ...
- 【学习笔记】--- 老男孩学Python,day10, 初识函数 形参、实参
函数:对功能的封装语法: def 函数名(形参): 函数体 函数名(实参) 函数名:命名规则和变量一样 函数的返回值: return, 函数执行完毕. 不会执行后面逻辑 1. 如果函数中不写retur ...
- cookie函数封装
var cookieUtil = { setCookie :function (cname, cvalue, exdays) { var exdate = new Date(); exdate.set ...
- 通过UA判断,对滚动条样式进行不同的操作
浏览器滚动条的默认样式比较丑,有些情况下,又不能直接overflow:hidden掉. 本文阐述如何通过 document.styleSheets[0].insertRule 简单的实现pc端和移动端 ...