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

最新文章

  1. Java下载文件(流的形式)
  2. vs2015帮助文档
  3. Asp.net MVC3表格共用分页功能
  4. 120. Triangle
  5. APP-PER-50022: Oracle Human Resources could not retrieve a value for the User Type profile option.
  6. Java-工厂设计模式
  7. bootstrap插件fileinput.js 出现出现$(&quot;#xxxx&quot;).fileinput({}); 不生效的情况解决
  8. 使用FFMPEG在windows平台下推rtmp流
  9. 如何在ASP.NET Core中自定义Azure Storage File Provider
  10. 查看open office运行状态
  11. js&#183;&#183;事件捕捉
  12. springboot----&gt;错误: 找不到或无法加载主类
  13. [转] LINUX内核代码编程规范
  14. SQLite 数据库增删改查
  15. HttpClinet学习笔记
  16. 【Java】代理模式,静态代理和动态代理(基于JDK或CGLib)
  17. 用LR录制文件下载并随机产生文件名
  18. [转]Ubuntu桌面入门指南
  19. bbbbbeta
  20. 如何更专业的使用Chrome开发者工具

热门文章

  1. python中不可变数据类型和可变数据类型
  2. 17-hadoop-yarn安装
  3. springboot-25-springboot 集成 ActiveMq
  4. springcloud-03-服务注册
  5. java序列化测试
  6. Idea 2017.3以后版本的破解(亲测有效)转
  7. 别被官方文档迷惑了!这篇文章帮你详解yarn公平调度
  8. 使用DAO模式开发宠物管理系统---hellokitty
  9. RabbitMQ 上手记录-part 1-基础概念
  10. volatile特性