ios多个异步请求回调后再执行操作

以前遇到的需求(见标题),自己实现了一下,贴上代码。包含了完整请求过程,拿来党请拿走。



#pragma mark  - 点击活动按钮,检测两个活动是否都是开放状态

-(void)checkIfActivitiesAreOn{

    __weak typeof(self)weakSelf = self;

    //信号量

    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, ^{

        [weakSelfpostActivityDailyInfoRequest:^{

            dispatch_semaphore_signal(semaphore);

        }];

    });

    //网络请求二

    dispatch_group_async(group, queue, ^{

        [weakSelfpostActivityWheelFortuneRequest:^{

            dispatch_semaphore_signal(semaphore);

        }];

    });


    dispatch_group_notify(group, queue, ^{

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);


        dispatch_async(dispatch_get_main_queue(), ^{

            if (weakSelf.treasureChestActivityIsOn && weakSelf.turnTableActivityIsOn) {

                GBActivityMainView *view = [GBActivityMainView invitationUpdateAppView];

                [viewshowFillInvitationUpdateAppView];

            }

        });

    });

}

#pragma mark - 获取活动1

- (void)postActivityDailyInfoRequest:(void(^)())finish

{

//    finish();

    __weak typeof(self)weakSelf = self;

    GBActivityDailyInfoRequest *req = [GBActivityDailyInfoRequest new];

    req.token = [GBFacade sharedInstance].currentUserInfo.token;

    [reqstartRequest:^(BOOLsuccess,HttpReqResult*result) {

        if(success){

            GBActivityDailyInfoDataInfo *info = result.resultObj[KEY_OBJECT];

            if(info.status==1) {// 0未开始,1进行中,2已结束

                weakSelf.treasureChestActivityIsOn=true;

            }else{

                weakSelf.treasureChestActivityIsOn=false;

            }

        }else{

            weakSelf.treasureChestActivityIsOn = false;

        }

        finish();

    }];

}

#pragma mark - 获取活动2

- (void)postActivityWheelFortuneRequest:(void(^)())finish

{

    __weak typeof(self)weakSelf = self;

    GBActivityWheelFortuneInfoRequest *req = [GBActivityWheelFortuneInfoRequest new];

    req.token = [GBFacade sharedInstance].currentUserInfo.token;

    [reqstartRequest:^(BOOLsuccess,HttpReqResult*result) {

        if(success){

            GBActivityWheelFortuneInfoDataInfo *info = result.resultObj[KEY_OBJECT];

            if(info.status==1) {// 0未开始,1进行中,2已结束

                weakSelf.turnTableActivityIsOn=true;

            }else{

                weakSelf.treasureChestActivityIsOn=false;

            }

        }else{

            weakSelf.treasureChestActivityIsOn = false;

        }

        finish();

    }];

}

你可能感兴趣的:(ios多个异步请求回调后再执行操作)