• 添加依赖后,队列中网络请求任务有依赖关系时,任务结束判定以数据返回为准还是以发起请求为准?
  • waitUntilFinished方法容易误解。

依赖关系

//
// ViewController.m
// OperationTest0108
//
// Created by LongMa on 2020/1/8.
// #import "ViewController.h"
#import <AFNetworking/AFNetworking.h> @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
[self testQueue];
} - (void)testQueue{
NSOperationQueue *lQ = [[NSOperationQueue alloc] init]; //任务最大并发数,与是否开启子线程无关。
// lQ.maxConcurrentOperationCount = 1; NSBlockOperation *lOp0 = [NSBlockOperation blockOperationWithBlock:^{
AFHTTPSessionManager *lMng = [AFHTTPSessionManager manager];
[lMng POST:@"https://www.baidu.com" parameters:@{
@"mapId" : @"1"
} progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"0 suc");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"0 error");
}]; NSLog(@"0 %@", [NSThread currentThread]);
}]; NSBlockOperation *lOp1 = [NSBlockOperation blockOperationWithBlock:^{
AFHTTPSessionManager *lMng = [AFHTTPSessionManager manager];
[lMng POST:@"https://www.baidu.com" parameters:@{
@"mapId" : @"1"
} progress:^(NSProgress * _Nonnull uploadProgress) { } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"1 suc");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"1 error");
}]; NSLog(@"1 %@", [NSThread currentThread]);
}]; NSBlockOperation *lOp2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2 %@", [NSThread currentThread]);
}]; [lOp0 addDependency:lOp1]; NSLog(@"before add op"); [lQ addOperations:@[lOp0] waitUntilFinished:NO];
[lQ addOperations:@[lOp1] waitUntilFinished:NO];
[lQ addOperations:@[lOp2] waitUntilFinished:NO];
} @end

执行结果

2020-01-08 18:02:31.378260+0800 OperationTest0108[1583:527022] before add op
2020-01-08 18:02:31.378635+0800 OperationTest0108[1583:527045] 2 <NSThread: 0x283db43c0>{number = 4, name = (null)}
2020-01-08 18:02:31.379722+0800 OperationTest0108[1583:527047] 1 <NSThread: 0x283db4240>{number = 5, name = (null)}
2020-01-08 18:02:31.380265+0800 OperationTest0108[1583:527047] 0 <NSThread: 0x283db4240>{number = 5, name = (null)}
2020-01-08 18:02:31.915236+0800 OperationTest0108[1583:527022] 0 error
2020-01-08 18:02:31.921841+0800 OperationTest0108[1583:527022] 1 error

由上面log可知:任务结束判定以发起请求为准!数据返回是异步的,不受依赖关系影响!

waitUntilFinished方法

当把上面代码

[lQ addOperations:@[lOp0] waitUntilFinished:NO];

改为

[lQ addOperations:@[lOp0] waitUntilFinished:YES];

时,

log如下,没有正常执行操作:

2020-01-08 18:03:55.308276+0800 OperationTest0108[1587:527738] before add op

分析:

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.

当为YES时,当前线程被阻塞,直到被添加的操作执行完毕。上面代码使线程依赖于lOp0执行完毕,而lOp0的执行依赖于lOp1执行完毕。由于lOp1比lOp0加入队列更晚。当上面代码被执行时,线程在等lOp0执行完毕,而此时lOp1还没被加入队列中,即lOp1还没开始执行,所以线程一直处于阻塞状态!当然,合理利用waitUntilFinished方法,也能实现想要的特殊效果。

最新文章

  1. 1. ReactNative 基础
  2. color 的一些处理
  3. 一分钟制作U盘版BT3 - 有图滴儿 bt3破解教程
  4. ffmpeg 音频转码
  5. 给定一颗二叉搜索树,请找出其中的第k小的结点。例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4。
  6. 不允许从数据类型 nvarchar 到 varbinary 的隐式转换
  7. canvas.js | CLiPS
  8. LINQ 基本子句之一 (select/where/group/into)
  9. Linux操作系统信息查看命令
  10. ORACLE 程序包
  11. 什么时候可以用delete替代delete[]
  12. java~lambda表达式让查询更优雅
  13. React native中使用XMLHttpRequest请求数据
  14. gitlab 500 服务器错误 重启解决了
  15. python系统编程(十一)
  16. nyoj 幸运三角形
  17. Django基础九之中间件
  18. hibernate的延迟加载和抓取策略
  19. 勉励自己--淡定的CrazyDog
  20. [剑指Offer] 47.求1+2+3+...+n

热门文章

  1. 生成所有2^n个长度为n的比特串
  2. @noi.ac - 171@ 立方体
  3. JS遍历数组
  4. 2019-10-5-dotnet-core-获取-MacAddress-地址方法
  5. 深入java面向对象四:Java 内部类种类及使用解析(转)
  6. pytorch nn.LSTM()参数详解
  7. 2018-2-13-C#-枚举转字符串
  8. Python--day46--分组(看了别人博客掌握的)
  9. Codeforces Round #198 (Div. 1 + Div. 2)
  10. (超级详细版)利用ThinkPHP3.2.3+PHPExcel实现将表格数据导入到数据库