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

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

  1. NSOperation的使用细节 [2]

    NSOperation的使用细节 [2] 这一节我们来写自定义nonconcurrent的operation,自定义nonconcurrent的operation很简单,重写main方法,之后处理好c ...

  2. NSOperation的使用细节 [1]

    NSOperation的使用细节 [1] NSOperation 使用起来并没有GCD直观,但它有着非常不错的面向对象接口,还可以取消线程操作,这一点是GCD所没有的,NSOperation本身是抽象 ...

  3. 认识和使用NSOperation

    原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...

  4. 多线程&NSObject&NSThread&NSOperation&GCD

    1.NSThread 每个NSThread对象对应一个线程,量级较轻(真正的多线程) 以下两点是苹果专门开发的“并发”技术,使得程序员可以不再去关心线程的具体使用问题 2.NSOperation/NS ...

  5. iOS之多线程开发NSThread、NSOperation、GCD

    原文出处: 容芳志的博客   欢迎分享原创到伯乐头条 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Centr ...

  6. NSOperation, NSOperationQueue 原理探析

    通过GNUstep的Foundation来尝试探索下NSOperation,NSOperationQueue 示例程序 写一个简单的程序 - (void)viewDidLoad { [super vi ...

  7. iOS多线程编程技术之NSThread、Cocoa NSOperation、GCD

    原文出处: 容芳志的博客 简介iOS有三种多线程编程的技术,分别是:(一)NSThread(二)Cocoa NSOperation(三)GCD(全称:Grand Central Dispatch) 这 ...

  8. 用NSOperation写下载队列

    用NSOperation写下载队列 说明 1. 支持缓存机制 2. 图片都是在主线程中加载 3. 文件名用了md5加密 *这东西被人写烂了,但大伙如果对NSOperation不熟悉的话,可以看看本人的 ...

  9. [New learn] NSOperation基本使用

    1.简介 NS(基于OC语言)是对GCD(基于C语言)的封装,让开发者能够更加友好的方便的去使用多线程技术. 2.NSOperation的基本使用 NSOperation是抽象类,所以如果要使用NSO ...

随机推荐

  1. windows 系统安装 IIS 以及配置 web

    Win7系统或win8自带了IIS和framework,不需要单独去下载,只需要开启后就可以运行asp.net程序 由于时间关系,先分享两个关于这个的链接: win7安装iis及web配置教程 htt ...

  2. Vue教程:windows下安装npm和cnpm

    [安装步骤] 一.安装node.js 1.前往node.js官网下载并安装工具,这里安装路径选到D盘,D:\Program Files\nodejs 安装完毕在命令行输入以下命令测试是否安装成功,正确 ...

  3. [日常] Go语言圣经-WEB服务与习题

    Go语言圣经-web服务 1.Web服务程序,标准库里的方法已经帮我们完成了大量工作 2.main函数将所有发送到/路径下的请求和handler函数关联起来,/开头的请求其实就是所有发送到当前站点上的 ...

  4. xmpp实现的即时通讯聊天(一)

    参考网址:http://www.jianshu.com/p/b401ad6ba1a7 http://www.jianshu.com/p/4edbae55a07f 一.mysql和openfire环境的 ...

  5. 啰里吧嗦jvm

    一.为什么要了解jvm 有次做项目的时候,程序run起来的时候,总是报OutOfMemoryError,有老司机教我们用jconsole.exe看内存溢出问题 就是这货启动jconsole后,发现一个 ...

  6. MYSQL与MSSQL对比学习

    最近在将公司的一个产品里面相关的MSSQL语句修改为可以在MYSQL上执行的语句 l  优点分析: MYSQL短小精悍,容易上手,操作简单,免费供用的.相对其它数据库有特色又实用的语法多一些.SQL怎 ...

  7. 响应式布局和BootStrap 全局CSS样式

    1.什么是响应式布局 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,这个概念是为解决移动互联网浏览而诞生的. 简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一 ...

  8. JAVA中的泛型(Generic)

    Java泛型(Generic)简介 泛型是jdk1.5版本以后推出来的,表示类型参数化,让java能更具有动态性一些,让类型能变成参数传递. 要我自己感觉的话,泛型本身没啥用,跟反射在一起用,就体现出 ...

  9. 设计模式学习——代理模式(Proxy Pattern)

    放假啦~学生们要买车票回家了,有汽车票.火车票,等.但是,车站很远,又要考试,怎么办呢?找代理买啊,虽然要多花点钱,但是,说不定在搞活动,有折扣呢~ /// /// @file Selling_Tic ...

  10. ajax获取富文本数据无法正常渲染到页面问题

    有时候富文本渲染到页面的时候 会连带标签一起渲染出来. 解决办法: 首先引用    <script src="https://cdn.jsdelivr.net/npm/fuwenben ...