一、NSOperation 抽象类

  • NSOperation 是一个"抽象类",不能直接使用。抽象类的用处是定义子类共有的属性和方法。
  • NSOperation 是基于 GCD 做的面向对象的封装。
  • 相比较 GCD 使用更加简单,并且提供了一些用 GCD 不是很好实现的功能。
  • 苹果公司推荐使用的并发技术。
  • 两个子类:
    • NSInvocationOperation (调用)
    • NSBlockOperation (块)

相比NSInvocationOperation推荐使用NSBlockOperation,代码简单,同时由于闭包性使它没有传参问题。

  • NSOperationQueue 队列

已经学习过的抽象类

  • UIGestureRecognizer
  • CAAnimation
  • CAPropertyAnimation

二、 NSOperation 和 GCD 的核心概念

  • GCD的核心概念:将 任务(block) 添加到队列,并且指定执行任务的函数。
  • NSOperation 的核心概念:将 操作 添加到 队列。

三、NSOperation 和 GCD的区别:

GCD

  • 将任务(block)添加到队列(串行/并发/主队列),并且指定任务执行的函数(同步/异步)
  • GCD是底层的C语言构成的API
  • iOS 4.0 推出的,针对多核处理器的并发技术
  • 在队列中执行的是由 block 构成的任务,这是一个轻量级的数据结构
  • 要停止已经加入 queue 的 block 需要写复杂的代码
  • 需要通过 Barrier 或者同步任务设置任务之间的依赖关系
  • 只能设置队列的优先级
  • 高级功能:
    • 一次性 once
    • 延迟操作 after
    • 调度组

NSOperation

  • 核心概念:把操作(异步)添加到队列(全局的并发队列)。
  • OC 框架,更加面向对象,是对 GCD 的封装。
  • iOS 2.0 推出的,苹果推出 GCD 之后,对 NSOperation 的底层全部重写。
  • Operation作为一个对象,为我们提供了更多的选择。
  • 可以跨队列设置操作的依赖关系
  • 可以设置队列中每一个操作的优先级
  • 高级功能:
    • 最大操作并发数(GCD不好做)
    • 继续/暂停/全部取消
    • 跨队列设置操作的依赖关系

四、代码实践

 //
// ViewController.m
// NSOperationTest
//
// Created by mayl on 2018/1/5.
// Copyright © 2018年. All rights reserved.
// #import "ViewController.h" @interface ViewController ()
@property(nonatomic, strong) NSOperationQueue *gOpQueue;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self setUpUI]; // [self aysncCon];
[self maxConCount];
// [self oftenUse];
// [self setUpDependence];
// [self waitUntilFinished];
} - (void)setUpUI{ //暂停,继续按钮
UIButton *lBtn4Pause = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:lBtn4Pause]; lBtn4Pause.frame = CGRectMake(, , , );
[lBtn4Pause setTitle:@"挂起" forState:UIControlStateNormal];
lBtn4Pause.titleLabel.numberOfLines = ;
[lBtn4Pause setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[lBtn4Pause addTarget:self action:@selector(pauseBtnDidClick:) forControlEvents:UIControlEventTouchUpInside]; //取消所有任务按钮
UIButton *lBtn4CancelAll = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:lBtn4CancelAll]; lBtn4CancelAll.frame = CGRectMake(, , , );
[lBtn4CancelAll setTitle:@"cancel all" forState:UIControlStateNormal];
[lBtn4CancelAll setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[lBtn4CancelAll addTarget:self action:@selector(cancelAllBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
} #pragma mark - action /**
队列挂起,当前"没有完成的操作",是包含在队列的操作数中的。
队列挂起,不会影响已经执行操作的执行状态。
队列一旦被挂起,再添加的操作不会被调度。
*/
- (void)pauseBtnDidClick:(UIButton *)btn{
NSLog(@"队列中操作数:%zd", self.gOpQueue.operationCount);
if ( == self.gOpQueue.operationCount) {
NSLog(@"队列中无操作");
return;
} NSLog(@"3:%d", self.gOpQueue.isSuspended);
self.gOpQueue.suspended = !self.gOpQueue.isSuspended;
NSLog(@"4:%d", self.gOpQueue.isSuspended);
if (self.gOpQueue.isSuspended) {
NSLog(@"队列挂起");
[btn setTitle:@"继续"
forState:UIControlStateNormal];
}else{
NSLog(@"队列继续");
[btn setTitle:@"挂起"
forState:UIControlStateNormal];
}
} /**
取消队列中所有的操作。
不会取消正在执行中的操作。
不会影响队列的挂起状态
*/
- (void)cancelAllBtnDidClick:(UIButton *)btn{
if ( == self.gOpQueue.operationCount) {
NSLog(@"队列中无操作");
return;
} NSLog(@"取消队列中所有操作,此方法不会改变队列挂起状态");
[self.gOpQueue cancelAllOperations]; NSLog(@"1:%d", self.gOpQueue.isSuspended);
self.gOpQueue.suspended = !self.gOpQueue.isSuspended;
NSLog(@"2:%d", self.gOpQueue.isSuspended);
} /** 默认是:异步,并发 */
- (void)aysncCon{
NSOperationQueue *lQueue = [[NSOperationQueue alloc] init];
for (int i = ; i < ; ++i) {
[lQueue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"%d,%@", i, [NSThread currentThread]);
}];
}
} /** 最大并发数:The maximum number of queued operations that can execute at the same time.*/
- (void)maxConCount{
NSOperationQueue *lQueue = [[NSOperationQueue alloc] init];
lQueue.maxConcurrentOperationCount = ;
for (int i = ; i < ; ++i) {
[lQueue addOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"%d,%@", i, [NSThread currentThread]);
}];
} self.gOpQueue = lQueue;
} /** 常用:子线程耗时,主线程更新UI */
- (void)oftenUse{
NSOperationQueue *lQ = [[NSOperationQueue alloc] init]; [lQ addOperationWithBlock:^{
NSLog(@"耗时操作开始,%@", [NSThread currentThread]);
[NSThread sleepForTimeInterval:];
NSLog(@"耗时操作结束"); [[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSLog(@"主线程更新UI,%@",
[NSThread currentThread]);
}]; }];
} /** 设置依赖 */
- (void)setUpDependence{
NSOperationQueue *lQ = [[NSOperationQueue alloc] init]; [lQ addOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"do something,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp1 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"1:登录,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp2 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"2:购买点券,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp3 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"3:使用点券,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp4 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"4:返回结果,%@",
[NSThread currentThread]);
}]; [lOp2 addDependency:lOp1];
[lOp3 addDependency:lOp2];
[lOp4 addDependency:lOp3]; //下面加的话会循环依赖,导致任何操作都无法进行,程序不会崩溃。
// [lOp1 addDependency:lOp4]; [lQ addOperations:@[lOp4, lOp3] waitUntilFinished:NO];
[lQ addOperations:@[lOp2, lOp1] waitUntilFinished:NO];
} /**执行效果如下:
2018-01-05 19:54:31.721539+0800 NSOperationTest[578:156322] come in
2018-01-05 19:54:33.727691+0800 NSOperationTest[578:156342] 0:do others,<NSThread: 0x1c027f740>{number = 3, name = (null)}
2018-01-05 19:54:34.731836+0800 NSOperationTest[578:156342] 1:登录,<NSThread: 0x1c027f740>{number = 3, name = (null)}
2018-01-05 19:54:35.737375+0800 NSOperationTest[578:156342] 2:购买点券,<NSThread: 0x1c027f740>{number = 3, name = (null)}
2018-01-05 19:54:36.742936+0800 NSOperationTest[578:156342] 3:使用点券,<NSThread: 0x1c027f740>{number = 3, name = (null)}
2018-01-05 19:54:37.746491+0800 NSOperationTest[578:156342] 4:show Time,<NSThread: 0x1c027f740>{number = 3, name = (null)}
2018-01-05 19:54:38.764408+0800 NSOperationTest[578:156341] 5:[lQ addOperations:@[lOp4, lOp3] waitUntilFinished:YES];实现了不设置依赖,且我需要最后执行,<NSThread: 0x1c04631c0>{number = 4, name = (null)}
*/
- (void)waitUntilFinished{
NSOperationQueue *lQ = [[NSOperationQueue alloc] init]; NSLog(@"come in");
NSBlockOperation *lOp0 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"0:do others,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp1 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"1:登录,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp2 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"2:购买点券,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp3 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"3:使用点券,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp4 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:];
NSLog(@"4:show Time,%@",
[NSThread currentThread]);
}]; NSBlockOperation *lOp5 = [NSBlockOperation blockOperationWithBlock:^{
[NSThread sleepForTimeInterval:]; NSLog(@"5:[lQ addOperations:@[lOp4, lOp3] waitUntilFinished:YES];实现了不设置依赖,且我需要最后执行,%@",
[NSThread currentThread]);
}]; [lOp2 addDependency:lOp1];
[lOp3 addDependency:lOp2];
[lOp4 addDependency:lOp3]; //执行顺序跟在数组中的顺序无关
//waitUntilFinished:If YES, the current thread is blocked until all of the specified operations finish executing. If NO, the operations are added to the queue and control returns immediately to the caller.(If YES,当前线程会被阻塞,直到数组中所有操作执行完毕。下局代码是直到lOp5执行完毕,才会执行后续操作)
[lQ addOperations:@[lOp0] waitUntilFinished:YES]; [lQ addOperations:@[lOp2, lOp1] waitUntilFinished:NO];
[lQ addOperations:@[lOp4, lOp3] waitUntilFinished:YES]; [lQ addOperation:lOp5];
} @end

多线程之NSOperation小结的更多相关文章

  1. 多线程之NSOperation

    关于多线程会有一系列如下:多线程之概念解析 多线程之pthread, NSThread, NSOperation, GCD 多线程之NSThread 多线程之NSOperation 多线程之GCD

  2. iOS多线程之NSOperation详解

    使用NSOperation和NSOperationQueue进行多线程开发,只要将一个NSOperation(实际开发中需要使用其子类 NSInvocationOperation,NSBlockOpe ...

  3. 多线程之NSOperation简介

    在iOS开发中,为了提升用户体验,我们通常会将操作耗时的操作放在主线程之外的线程进行处理.对于正常的简单操作,我们更多的是选择代码更少的GCD,让我们专注于自己的业务逻辑开发.NSOperation在 ...

  4. 多线程之NSOperation和NSOperationQueue

    这篇文章里我将不过多的谈及理论知识,这些东西会的自然会,不会的,看多了也是云里雾里.下面我讲更多的用代码+注释的方式来讲如何使用NSOperation和NSOperationQueue. 1.NSOp ...

  5. iOS-多线程之NSOperation

    前言 这篇文章主要讲NSOperation的使用. What 使用NSOperation和NSOperationQueue进行多线程开发类似于线程池,只要将一个NSOperation(实际开发中需要使 ...

  6. IOS多线程之NSOperation学习总结

    NSOperation简介 1.NSOperation的作用 配合使用NSOperation和NSOperationQueue也能实现多线程编程 2.NSOperation和NSOperationQu ...

  7. iOS多线程之NSOperation,NSOperationQueue

    使用 NSOperation的方式有两种, 一种是用定义好的两个子类: NSInvocationOperation 和 NSBlockOperation. 另一种是继承NSOperation 如果你也 ...

  8. (五十六)iOS多线程之NSOperation

    NSOpertation是一套OC的API,是对GCD进行的Cocoa抽象. NSOperation有两种不同类型的队列,主队列和自定义队列. 主队列运行于主线程上,自定义队列在后台运行. [NSBl ...

  9. iOS开发多线程之NSOperation

    NSInvocationOperation The NSInvocationOperationclass is a concrete subclass of NSOperationthat you u ...

随机推荐

  1. 《STL源码剖析》——List

    List list位于头文件<<stl_list.h>>中 list是sequence containers中的一种 1 List的基本架构 list的基本结构的UML关系图如 ...

  2. 雷神领域(并查集真是个好东西)并查集+流氓dp

    考场上,整整看了半个小时以上的题目!!! 化简题意: 给定一个全0矩阵,一些坐标点(x,y)为1,当三个点可以构成一个直角三角形时(直角边长为整数)拓展为一个矩形,之后从(0,0)出发,求最多的占用行 ...

  3. 浅谈 KMP 算法

    最近在复习数据结构,学到了 KMP 算法这一章,似乎又迷糊了,记得第一次学习这个算法时,老师在课堂上讲得唾沫横飞,十分有激情,而我们在下面听得一脸懵比,啥?这是个啥算法?啥玩意?再去看看书,完全听不懂 ...

  4. Python基本数据结构之字典

    定义: {key1:value1,key2:value2} 1.键与值用冒号“:”分开: 2.项与项用逗号“,”分开: 示例: dic4={ 'name': 'xiaohu', 'age': 20, ...

  5. python——直接插入排序

    直接插入排序 直接插入排序原理 直接插入排序过程 代码 时间复杂度分析 排序稳定性

  6. csp-s2019游记

    11.15D0: 复习 复习 机房里弥漫着颓废的气息,不过也是最后一个下午了 11.16D1: 五点钟爬起来,一边发抖一边去楼下买早饭 虽然平时基本不吃早饭,但考前不行 搭着同学的车去了考点,在车上重 ...

  7. python06-列表表达式、生成器表达式及其面试题、解耦简单介绍、函数递归相关

    目录: 一.列表推导式 二.生成器表达式 三.集合生成器 四.生成器面试题 五.解耦简单介绍 六.函数递归相关 一.列表推导式 需求:将[1,3,5]中的每个元素平方 正常思路: new_list = ...

  8. vue 前端处理监听关键字搜索

    根据组件的业务需要,有时候搜索是把关键字返回给后台,后台处理后再把数据返回给前端渲染(多次请求服务器):有时候是前端把页面的数据全部获取下来,前端处理关键字的搜索(影响页面加载) 我这个文章是介绍第二 ...

  9. CGI、FastCGI、CLI、Apache、ISAPI之PHP运行环境对比

    1.运行模式 关于PHP目前比较常见的五大运行模式: 1)CGI(通用网关接口 / Common Gateway Interface) 2)FastCGI(常驻型CGI / Long-Live CGI ...

  10. (C#)WPF:.h(头文件)、.lib(静态链接库文件)和.dll(动态链接库文件)之间的区别与联系

    静态链接库(Lib)与动态链接库(DLL)的区别 静态连接库就是把(lib)文件中用到的函数代码直接链接进目标程序,程序运行的时候不再需要其它的库文件:动态链接就是把调用的函数所在文件模块(DLL)和 ...