多个网络请求后,再执行某个 操作

方式1 dispatch_group_enter && dispatch_group_leave
 dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    [self getAdvertList:^(BOOL iscomple) {
        dispatch_group_leave(group);
    }];
    // requset2
    dispatch_group_enter(group);
    [self getHotCultureList:^(BOOL iscomple) {
        dispatch_group_leave(group);
    }];
 
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        //等待requset1, requset2均调用dispatch_group_leave则进入dispatch_group_notify
     }
 

方式2 dispatch_semaphore_t

//信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    //创建全局并行
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //任务一
    dispatch_group_async(group, queue, ^{
        [self getAdvertList:^(BOOL iscomple) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
    //任务二
    dispatch_group_async(group, queue, ^{
        [self getHotCultureList:^(BOOL iscomple) {
            dispatch_semaphore_signal(semaphore);
        }];
    });
 
 
    dispatch_group_notify(group, queue, ^{
 
        //6个任务,6个信号等待.
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        //这里就是所有异步任务请求结束后执行的代码
        //[self.homeTableView.mj_header endRefreshing];
        //这里两个网络请求结束后。获取到的。一个参数用于第三个借口参数
        NSLog(@"------------4444444444444444-xin hao-------%@", self.imeiStr);

};

你可能感兴趣的:(多个网络请求后,再执行某个 操作)