• iOS 控制网络请求顺序


    顺序执行

    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");
        }];
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    很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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    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(@"任务完成");
        });
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    循环请求

    我是用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];
            });
        });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    在ubuntu16.04系统利用eBPF获取TCP网络状态信息
    案例研究丨神策数据在多项目、多网络场景下使用JumpServer堡垒机
    分布式事务Seata
    让你快速理解工厂模式
    uniapp封装接口
    vue小技能:使用渲染函数编写组件
    iOS APP 转让避坑指南
    RedisConnectionException: Unable to connect to 127.0.0.1:6379
    2022年湖北助理工程师职称评审费用是多少?多久出证呢?甘建二
    Go 语言结构体验证详解:validate 标签与自定义规则
  • 原文地址:https://blog.csdn.net/m0_63852285/article/details/127972883