当前位置: 首页 > 工具软件 > Queue for iOS > 使用案例 >

iOS 控制网络请求顺序

段干德泽
2023-12-01

顺序执行

  1. 不使用GCD,直接在网络请求的回调中在发送请求。
	LastStoriesModel* lastStoriesModel = [_lastStoriesModelArray lastObject];
    NSString* lastDate1 = lastStoriesModel.date;
    NSString* lastDate2 = [DateTool dateMinusOneWhithTimeString:lastDate1];
    NSString* lastDate3 = [DateTool dateMinusOneWhithTimeString:lastDate2];
    
    [_manage getLastTime:lastDate1 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
        [self->_lastStoriesModelArray addObject:lastStoriesModel];
        [self sendStoriserToView:lastStoriesModel];
        
        [self->_manage getLastTime:lastDate2 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
            [self->_lastStoriesModelArray addObject:lastStoriesModel];
            [self sendStoriserToView:lastStoriesModel];
            
            [self->_manage getLastTime:lastDate3 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
                [self->_lastStoriesModelArray addObject:lastStoriesModel];
                [self sendStoriserToView:lastStoriesModel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->_interFaceView viewInit];
                });
            } error:^(NSError * _Nonnull error) {
                NSLog(@"getLastModel error");
            }];
        } error:^(NSError * _Nonnull error) {
            NSLog(@"getLastModel error");
        }];
    } error:^(NSError * _Nonnull error) {
        NSLog(@"getLastModel error");
    }];

很low,请求一多就没法写。但请求少时还是很实用的。

  1. 使用dispatch_group_async、dispatch_group_notify。多个网络请求同时进行,等请求所有网络请求完在操作。
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
   		 [self->_manage getLastTime:lastDate3 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
                [self->_lastStoriesModelArray addObject:lastStoriesModel];
                [self sendStoriserToView:lastStoriesModel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->_interFaceView viewInit];
					 NSLog(@"任务一完成");
                });
            } error:^(NSError * _Nonnull error) {
                NSLog(@"getLastModel error");
            }];
    });
    
    dispatch_group_async(group, queue, ^{
    	...
        NSLog(@"任务二完成");
    });
    
    dispatch_group_async(group, queue, ^{
    	...
        NSLog(@"任务三完成");
    });
    //在分组的所有任务完成后触发
    dispatch_group_notify(group, queue, ^{
    	...
        NSLog(@"所有任务完成");
    });
  1. 上也可以使用dispatch_group_enter/leave/wait
 
-(void)serialByGroupWait {
    
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_group_enter(group);
   	[self->_manage getLastTime:lastDate1 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
   				dispatch_group_leave(group);
                [self->_lastStoriesModelArray addObject:lastStoriesModel];
                [self sendStoriserToView:lastStoriesModel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->_interFaceView viewInit];
					 NSLog(@"任务一完成");
                });
            } error:^(NSError * _Nonnull error) {
            	dispatch_group_leave(group);
                NSLog(@"getLastModel error");
            }];
    
    dispatch_group_wait(group, DISPATCH_TIME_FOREVER);// 1执行完 下面才会执行
    
    dispatch_group_enter(group);
   	[self->_manage getLastTime:lastDate2 StoriesData:^(LastStoriesModel * _Nonnull lastStoriesModel) {
   				dispatch_group_leave(group);
                [self->_lastStoriesModelArray addObject:lastStoriesModel];
                [self sendStoriserToView:lastStoriesModel];
                dispatch_async(dispatch_get_main_queue(), ^{
                    [self->_interFaceView viewInit];
					 NSLog(@"任务二完成");
                });
            } error:^(NSError * _Nonnull error) {
            	dispatch_group_leave(group);
                NSLog(@"getLastModel error");
            }];
    
  // 1 2都完成 才会执行
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        NSLog(@"任务完成");
    });
}
 

循环请求

我是用dispatch_group_enter/leave/wait控制请求顺序。

dispatch_group_t group = dispatch_group_create();
    
    for (NSString* ID in self.myCollectModel.collectSet) {
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
        dispatch_group_enter(group);
        [[Manage sharedManage] getCollectConentWithID:ID CollectData:^(MyCollectContentModel * _Nonnull myCollectContenModel) {
            [self.myCollectModel.collectTitle addObject:myCollectContenModel.title];
            [self.myCollectModel.collectImagePath addObject:myCollectContenModel.image];
            dispatch_group_leave(group);
        } error:^(NSError * _Nonnull error) {
            NSLog(@"get Collect error");
            dispatch_group_leave(group);
        }];
    }
    
    dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            self.myCollectView.titleArray = self.myCollectModel.collectTitle;
            self.myCollectView.imagePathArray = self.myCollectModel.collectImagePath;
            
            [self.myCollectView viewReload];
        });
    });
 类似资料: