本文介绍: 很low请求一多就没法写。但请求少时还是很实用的。

顺序执行

  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_asyncdispatch_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];
        });
    });

原文地址:https://blog.csdn.net/m0_63852285/article/details/127972883

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_29292.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注