美文网首页
GCD之group正确用法

GCD之group正确用法

作者: 米匠 | 来源:发表于2019-10-22 13:57 被阅读0次

    等待多个异步操作结束,再做汇总操作:

        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_group_t group = dispatch_group_create();
    
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            [[HttpRequest sharedRequest] getWithURLString:api_applyListQuery_queryApplySchedule parameters:@{} success:^(id responseObject) {
                
                dispatch_group_leave(group);
                
            } failure:^(NSError *error) {
                
                dispatch_group_leave(group);
                
            }];
        });
    
        dispatch_group_enter(group);
        dispatch_group_async(group, queue, ^{
            [[HttpRequest sharedRequest] getWithURLString:api_applyListQuery_queryApplySchedule parameters:@{} success:^(id responseObject) {
                
                dispatch_group_leave(group);
                
            } failure:^(NSError *error) {
                
                dispatch_group_leave(group);
                
            }];
        });
    
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            // 汇总操作
        });
    

    注意:
    dispatch_group_enter(group);dispatch_group_leave(group); 要一一对应,否则会崩溃

    相关文章

      网友评论

          本文标题:GCD之group正确用法

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