iOS-一个页面多个请求完成以后再执行其他操作

前言

我们在开发的过程中,大家应该都会遇到已进入某个页面,就要请求多个API,然后我们在完成所有请求以后再进行其他操作,对于这种需求,我们如何来设计我们的代码呢?

例如下面的场景,在发现的页面有6个模块,但是后端给的接口又是分别不同的接口来调用。



实战

我所有的网络请求是基于AFNetworking的封装,然后我给所以的网络请求方法加了一个请求结束的回调,我以头部获取广告数组为例,代码如下:

#pragma mark - 获取广告数组
- (void)getAdHotTopDataBaseRequestisScu:(void(^)(BOOL isScu))requestisScu{
    WEAKBLOCK;
    [self.topAddArray removeAllObjects];
    NSString *urlStr = @"Ad/fx_top";
    [LFNetWorkManager requestDataBaseWithURLByGET:urlStr parameters:nil isWithToken:NO completed:^(id responseObject) {
        if([responseObject[@"code"] integerValue] == 200){
             [ToosZFJ readAndWriteHomeListDataBase:responseObject fileName:[urlStr stringFromMD5] completed:nil];//这个是我写的缓存数据的方法
            NSArray *data = responseObject[@"data"];
            for (NSDictionary *dict in data) {
                ADModel *model = [[ADModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                model.mid = dict[@"id"];
                if([dict[@"circle"] integerValue] == 1){
                    //话题
                }else{
                    //头部轮播广告
                    [weakSelf.topAddArray addObject:model];
                }
            }
        }else{
            [weakSelf SHOWPrompttext:responseObject[@"message"]];
        }
        if(requestisScu){
            requestisScu((200 == [responseObject[@"code"] integerValue])?YES:NO);
        }
    }];
}

备注:200代表请求成功,其他的是请求失败!

其他的网络请求大致如此,加一个回调来通知我请求是否结束,不论失败与否

然后就是把所以的网络请求放在一个函数里,使用dispatch_group_t来安排请求顺序,代码如下:

#pragma mark - 开始进行请求数据
- (void)startAllRequest{
    [self showMBProgressHUDLoding:nil];
    
    WEAKBLOCK;
    
    dispatch_group_t group = dispatch_group_create();
    
    // 广告数组
    dispatch_group_enter(group);
    [self getAdHotTopDataBaseRequestisScu:^(BOOL isScu) {
        dispatch_group_leave(group);
    }];
    
    // 我的关注
    dispatch_group_enter(group);
    [self getDataOfSOurcesFoucesRequestisScu:^(BOOL isScu) {
        dispatch_group_leave(group);
    }];
    
    // 推荐比赛
    dispatch_group_enter(group);
    [self getDataSourcesCompareRequestisScu:^(BOOL isScu) {
        dispatch_group_leave(group);
    }];
    
    //推荐文章
    dispatch_group_enter(group);
    [self getDataSourcesArticleRequestisScu:^(BOOL isScu) {
        dispatch_group_leave(group);
    }];
    
    //推荐老师
    dispatch_group_enter(group);
    [self pageDataAllwithPageRequestisScu:^(BOOL isScu) {
        dispatch_group_leave(group);
    }];
    
    //推荐机构
    dispatch_group_enter(group);
    [self dealWithOrganizationDataRequestisScu:^(BOOL isScu) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [weakSelf RemoveMBProgressHUDLoding:nil];
        [weakSelf.collectionView reloadData];
        [weakSelf endRefreshingForTableView:weakSelf.collectionView];
    });
}

等待所有的网络请求调用dispatch_group_leave以后,就进入dispatch_group_notify,然后我们就可以刷新我们的界面了。

刷新如下:

self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //所以需要请求的数据
        [self startAllRequest];
    }];

结束语

最近好久没有更新了,迷上了一款小游戏TownShip大笑











猜你喜欢

转载自blog.csdn.net/u014220518/article/details/78605329