美文网首页
iOS 多任务异步请求 信号量

iOS 多任务异步请求 信号量

作者: 陈先生的干货店 | 来源:发表于2021-05-31 17:39 被阅读0次

    开发过程中我们常碰到一个界面有请求多个接口,可以这样操作:

    1.这个适用于各个请求不相关联:

    -(void)requestAll_GCDSemaphore{
        //创建信号量
        //下面都是示范接口
      
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
        
        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, ^{
            //1
            NSDictionary *param = @{@"cate":@"1"};
            [ASNetworkingSerivce postRequestWithApi:@"http://zl.kobi.shop/api/index/yuedu_list" param:param success:^(NSDictionary * _Nonnull rootDict) {
                NSLog(@"1--------\n%@",rootDict);
                dispatch_semaphore_signal(semaphore);
            } failure:^(id  _Nonnull error) {
                NSLog(@"1--------\n%@",@"请求失败");
                dispatch_semaphore_signal(semaphore);
            }];
        });
        
        dispatch_group_async(group, queue, ^{
            //2
            NSDictionary *param = @{@"cate":@"1",@"page":@"2"};
            [ASNetworkingSerivce postRequestWithApi:@"http://zl.kobi.shop/api/index/yuedu_list" param:param success:^(NSDictionary * _Nonnull rootDict) {
                NSLog(@"2--------\n%@",rootDict);
                dispatch_semaphore_signal(semaphore);
            } failure:^(id  _Nonnull error) {
                NSLog(@"2--------\n%@",@"请求失败");
                dispatch_semaphore_signal(semaphore);
            }];
    
        });
        
        dispatch_group_async(group, queue, ^{
            //3
            NSDictionary *param = @{@"cate":@"1",@"page":@"3"};
            [ASNetworkingSerivce postRequestWithApi:@"http://zl.kobi.shop/api/index/yuedu_list" param:param success:^(NSDictionary * _Nonnull rootDict) {
                NSLog(@"3--------\n%@",rootDict);
                dispatch_semaphore_signal(semaphore);
            } failure:^(id  _Nonnull error) {
                NSLog(@"3--------\n%@",@"请求失败");
            }];
            
        });
        //这里会等待3个接口请求成功或者失败之后 才去通知主线程的刷新
        dispatch_group_notify(group, queue, ^{
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
            NSLog(@"信号量为0");
            dispatch_async(dispatch_get_main_queue(), ^{
                // 通知主线程刷新
                
            });
        });
    }
    
    
    

    2.相关联的请求 :

    使用场景,比如顶部有多个tab,这个时候我们需要先从后台请求tab,然后再去请求每个tab下面的列表

    - (void)requestAppDataSourceSuccess:(void(^)(void))success failure:(void(^)(void))failure {
        __weak typeof(self) weakSelf = self;
       //首先课定义一个block回调,这里面就是等待tab请求完成后再来执行block里面的代码
        void(^loadGoodsList)(void) = ^{
            ASXianYuGroupModel *groupModel = weakSelf.groupList.firstObject;
            NSDictionary *param = @{@"taoBao2GoodsGroupId":groupModel.taoBao2GoodsGroupId,@"pageNum":@"0",@"pageSize":@"20"};
            [ASNetworkingSerivce getRequestWithApi:@"http://test.api.doushangzhitongche.com/dsztc/app/taobao2/goods/list-app" param:param success:^(NSDictionary * _Nonnull rootDict) {
                if ([rootDict objectForKey:@"code"]) {
                    NSInteger code = [rootDict[@"code"] integerValue];
                    if (code == 200) {
                        weakSelf.sourceList = [ASXianYuListModel mj_objectArrayWithKeyValuesArray:rootDict[@"rows"]];
                        CGFloat cellW = ([UIScreen mainScreen].bounds.size.width - 50.f) / 2;
                        for (ASXianYuListModel *sourceModel in weakSelf.sourceList) {
                            UIImage *image = [[SDImageCache sharedImageCache] imageFromCacheForKey:sourceModel.topPictureUrl];
                            if (image) {
                                sourceModel.imageSize = image.size;
                            }else {
                                sourceModel.imageSize = CGSizeMake(100.f, 200.f);
                            }
                            CGSize lblSize = [sourceModel.title boundingRectWithSize:CGSizeMake(cellW - 20.f, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
                            sourceModel.textHeight = lblSize.height;
                        }
                    }
                }
                success();
            } failure:^(id  _Nonnull error) {
                failure();
            }];
        };
        
    //首先请求tab列表的接口
        [ASNetworkingSerivce getRequestWithApi:tabUrl param:@{} success:^(NSDictionary * _Nonnull rootDict) {
            if ([rootDict objectForKey:@"code"]) {
                NSInteger code = [rootDict[@"code"] integerValue];
                if (code == 200) {
                    // 请求tab接口成功之后  再去调用上面blcok
                     loadGoodsList();
                }
            }
           
        } failure:^(id  _Nonnull error) {
            failure();
        }];
    }
    
    

    调用

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        __weak typeof(self) weakSelf = self;
        [self requestAppDataSourceSuccess:^{
            NSLog(@"%@",weakSelf.sourceList);
            [weakSelf.collectionView reloadData];
        } failure:^{
                
        }];
    }
    
    

    相关文章

      网友评论

          本文标题:iOS 多任务异步请求 信号量

          本文链接:https://www.haomeiwen.com/subject/vsmbsltx.html