NSOperationQueue队列中操作依赖相关思考

移动开发 作者: 2024-08-24 18:30:01
添加依赖后,队列中网络请求任务有依赖关系时,任务结束判定以数据返回为准还是以发起请求为准? waitUntilFinished方法容易误解。 依赖关系 // // ViewController.m /
  • 添加依赖后,队列中网络请求任务有依赖关系时,任务结束判定以数据返回为准还是以发起请求为准?
  • 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

[lQ addOperations:@[lOp0] waitUntilFinished:NO];
[lQ addOperations:@[lOp0] waitUntilFinished:YES];
2020-01-08 18:03:55.308276+0800 OperationTest0108[1587:527738] before add op
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_67961.html