美文网首页网络
iOS实现多个网络请求ABC执行完再执行D

iOS实现多个网络请求ABC执行完再执行D

作者: 赵哥窟 | 来源:发表于2020-06-15 11:06 被阅读0次

    方案1:使用group和semaphore
    方案2:group_enter和group_leave也可以实现

    方案1实现例子

     dispatch_semaphore_t semaphore = dispatch_semaphore_create(2);
            dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
             
            //任务A
            dispatch_async(quene, ^{
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
               [self requestA:^{
                   NSLog(@"---执行A任务结束---");
               }];
                dispatch_semaphore_signal(semaphore);
            });
            //任务B
            dispatch_async(quene, ^{
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                 [self requestB:^{
                              NSLog(@"---执行B任务结束---");
                          }];
                dispatch_semaphore_signal(semaphore);
            });
            //任务C
            dispatch_async(quene, ^{
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                 [self requestC:^{
                     NSLog(@"---执行C任务结束---");
                }];
                dispatch_semaphore_signal(semaphore);
            });
            //任务D
            dispatch_async(quene, ^{
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                 [self requestD:^{
                     NSLog(@"---执行D任务结束---");
                }];
                dispatch_semaphore_signal(semaphore);
            });
    

    方案2实现例子

      dispatch_queue_t globalQueue = dispatch_get_global_queue(0, 0);
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_enter(group);
        [self requestA:^{
            NSLog(@"---执行A任务结束---");
            dispatch_group_leave(group);
        }];
        
        dispatch_group_enter(group);
        [self requestB:^{
            NSLog(@"---执行B任务结束---");
            dispatch_group_leave(group);
        }];
        
        dispatch_group_enter(group);
        [self requestC:^{
            NSLog(@"---执行C任务结束---");
            dispatch_group_leave(group);
        }];
        
        dispatch_group_notify(group, globalQueue, ^{
            [self requestD:^{
                NSLog(@"---执行D任务结束---");
            }];
        });
    
    
    - (void)requestA:(void(^)(void))block{
          NSLog(@"---执行A任务开始---");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            block();
        });
    }
    - (void)requestB:(void(^)(void))block{
          NSLog(@"---执行B任务开始---");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            block();
        });
    }
    - (void)requestC:(void(^)(void))block{
          NSLog(@"---执行C任务开始---");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            block();
        });
        
    }
    - (void)requestD:(void(^)(void))block{
        NSLog(@"---执行D任务开始---");
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            block();
        });
    }
    

    相关文章

      网友评论

        本文标题:iOS实现多个网络请求ABC执行完再执行D

        本文链接:https://www.haomeiwen.com/subject/ygrcxktx.html