iOS分组并发网络请求

2019-05-12  本文已影响0人  雾霭天涯

需求1:有多个网络请求,要求所有网络请求全部完成后,进行页面刷新等操作
需求2:有多个网络请求,且要求网络请求顺序依次执行,后面的网络请求依赖前面的网络请求结果等,所有网络请求全部完成后,进行页面刷新等操作

多任务无顺序

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
   
   dispatch_group_t group = dispatch_group_create();
  
   dispatch_group_enter(group);
   dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       //请求1 此处为分线程
       [self requestMethodWithGroup:group withIndex:1];
   });
   
   dispatch_group_enter(group);
   dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       //请求2
       [self requestMethodWithGroup:group withIndex:2];
   });
   
   dispatch_group_enter(group);
   dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       //请求3
       [self requestMethodWithGroup:group withIndex:3];
   });
   
   dispatch_group_notify(group, dispatch_get_main_queue(), ^{
       //界面刷新  此处为主线程
       NSLog(@"任务均完成,刷新界面");
   });
}
- (void)requestMethodWithGroup:(dispatch_group_t)thegroup withIndex:(NSInteger)aIndex
{
   [网络请求:{
       NSLog(@"第 %@次请求",@(aIndex));
       dispatch_group_leave(thegroup);
   }];
}

多任务有顺序

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1.任务一:下载图片
NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{
[self requestMethodWithIndex:1];
}];

//2.任务二:打水印
NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{
[self requestMethodWithIndex:2];
}];

//3.任务三:上传图片
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{
[self requestMethodWithIndex:3];
}];

//4.设置依赖
[operation2 addDependency:operation1];      //任务二依赖任务一
[operation3 addDependency:operation2];      //任务三依赖任务二

//5.创建队列并加入任务
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperations:@[operation3, operation2, operation1] waitUntilFinished:NO];
}

- (void)requestMethodWithIndex:(NSInteger)aIndex
{   
//此处为分线程
//注意 如果此处不通过信号量进行限制,虽然 线程设置了 相互依赖关系,但是请求的结果异步 仍然不是顺序打印的,是乱序的
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[网络请求:{
       NSLog(@"第 %@次请求",@(aIndex));
         dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

使用协程coobjc实现 多任务有顺序

#import <coobjc/coobjc.h>

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    co_launch(^{
        NSArray* array1  = await([self requestMethodWithIndex:1]);
        NSArray* array2  = await([self requestMethodWithIndex:2]);
        NSArray* array3  = await([self requestMethodWithIndex:3]);
        //当前线程为主线程
        NSLog(@"任务均完成,刷新界面 %@",array1);
    });
    
}
- (COPromise*)requestMethodWithIndex:(NSInteger)aIndex
{
    COPromise *promise = [COPromise promise];
    [[MaintainDeclareDataTool shareInstance] getDicWithType:@"Area" result:^(NSArray *array,NSString *err) {
        NSLog(@"第 %@次请求->-个数 %@",@(aIndex),@(array.count));
        [promise fulfill:array];
    }];
    return promise;
}


上一篇下一篇

猜你喜欢

热点阅读