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 ...
随机推荐
- C语言中判断字符串str1是否以str2开始或结束
#include <stdlib.h> #include <string.h> #include <stdio.h> /**判断str1是否以str2开头 * 如果 ...
- cpu负载的探讨
原链接:http://blog.chinaunix.net/uid-12693781-id-368837.html 摘要:确定cpu的负载的定义,帮助管理员设置cpu负载阀值,推测可能的导致cpu负载 ...
- lucene源码分析(1)基本要素
1.源码包 core: Lucene core library analyzers-common: Analyzers for indexing content in different langua ...
- 表单提交.serialize()方法
html中<form id="myForm" action="..." method='POST'> <div><input ty ...
- Linux下最常用的Shell命令的介绍
Shell基础: 你可以通过打开Linux的terminal(终端)来执行Shell命令.Shell的种类有很多种,例如CSH,Bourne Shell,Korn Shell.在现在的大多数Linux ...
- CUBA 7 新特性(上篇)
三年前,我们宣布了 CUBA 框架的第二个公开的主版本.CUBA 6 是改变游戏规则的版本 - 框架的许可从私有化变成了公开的 Apache2.0.那些日子里,我们甚至猜不到这个变化会最终将框架带向何 ...
- 通向全栈之路——(3)node环境搭建
1:更新系统 sudo apt-get update2:安装相关软件 sudo apt-get install vim openssl build-essential libssl-dev wget ...
- Struts初始
1.首先我们先创建一个maven的简单工程, 如图 然后点击创建一个简单的工程,点击下一步, 然后, 再次输入工程的各项信息,1组织名,2.项目名称,版本名,一般为默认,3,jar包暂时默认,当前的j ...
- C#动态创建Gridview及批量插入到数据库
这里介绍两种动态创建Gridview的方法: (一).有时需要应付上头领导的检查,所以就弄一些静态的Gridview来显示数据,这种方法的优点就是不用连接数据库,比较方便,但是代码灵活性不高,所有数据 ...
- [C语言] 数据结构-离散存储链表定义
离散存储[链表] 1.定义: n个节点离散分配,彼此通过指针相连 每个节点只有一个前驱节点 只有一个后续节点 首节点没有前驱节点,尾节点没有后续节点 2.专业术语: 首节点:第一个有效节点 尾节点:最 ...