GCD在AFN中等待多个请求完成执行下个请求

有六个请求,前五个请求为异步请求,而第六个请求需要拿到前五个请求的数据再执行. 本来这种请求按照书上写的这种形式就可以.如:

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        dispatch_group_t group = dispatch_group_create();
        
        dispatch_group_async(group, queue, ^{
            [self onePositiveImageUpload:self.positiveImageView.image];
        });
        dispatch_group_async(group, queue, ^{
            [self oneReverseImageUpload:self.reverseImageView.image];
        });
        dispatch_group_async(group, queue, ^{
            [self moreFirstImageUploadWithArray:self.houseCardArray];
        });
        dispatch_group_async(group, queue, ^{
            [self moreSecondImageUploadWithArray:self.leaseArray];
        });
        dispatch_group_async(group, queue, ^{
            [self moreThirdImageUploadWithArray:self.applicantArray];
        });
        
//等待上面请求完成之后再执行里面的步骤
        dispatch_group_notify(group, queue, ^{
            [self requestSaveHouseMaterial];
        });
    });

但是项目中我们一般都是使用AFN请求,此时再使用这种格式就不可以了.会造成最后一个请求开始时拿到的没有拿到前几个请求完成之后的数据.此时就可以使用这种方法了

创建一个全局的group
dispatch_group_t _group;

在前几个请求中都加入 dispatch_group_enter(_group); 成功或失败时加入dispatch_group_leave(_group); 如:
dispatch_group_enter(_group);
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html", nil];
    // 2.封装参数(这个字典只能放非文件参数)
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    // 2.发送一个请求
    [mgr POST:UPLOAD_IMAGE_URL parameters:params constructingBodyWithBlock:^(id formData) {
        NSMutableArray *arr = [NSMutableArray array];
        for (int i = 0; i < array.count; i ++)
        {
            NSData *fileData = UIImageJPEGRepresentation(array[i], 0.05);
            [arr addObject:fileData];
        }
        for (int i = 0; i < arr.count; i ++)
        {
            [formData appendPartWithFileData:arr[i] name:[NSString stringWithFormat:@"filestream%d", i] fileName:[NSString stringWithFormat:@"zhengshu%d.jpg", i] mimeType:@"image/jpeg"];
        }
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *data = responseObject[@"data"];
        // 图片数组
        NSArray *array = [data[@"fileName"] componentsSeparatedByString:@","];
        NSMutableArray *urlArr = [NSMutableArray array];
        for (int i = 0; i < array.count; i ++)
        {
            NSString *url = [array[i] lastPathComponent];
            [urlArr addObject:url];
        }
        self.thirdString = [urlArr componentsJoinedByString:@","];
        dispatch_group_leave(_group);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [SMGlobalMethod showMessage:NetNoti];
        dispatch_group_leave(_group);
    }];

请求的时候这样写就OK了.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
        _group = dispatch_group_create();
        
        dispatch_group_async(_group, queue, ^{
            [self onePositiveImageUpload:self.positiveImageView.image];
        });
        dispatch_group_async(_group, queue, ^{
            [self oneReverseImageUpload:self.reverseImageView.image];
        });
        dispatch_group_async(_group, queue, ^{
            [self moreFirstImageUploadWithArray:self.houseCardArray];
        });
        dispatch_group_async(_group, queue, ^{
            [self moreSecondImageUploadWithArray:self.leaseArray];
        });
        dispatch_group_async(_group, queue, ^{
            [self moreThirdImageUploadWithArray:self.applicantArray];
        });
        
        //因上面请求中有加入dispatch_group_enter 和 dispatch_group_leave,所以真正等待上面线程执行完才执行这里面的请求
        dispatch_group_notify(_group, queue, ^{
            [self requestSaveHouseMaterial];
        });
    });
这样我们使用AFN请求数据的时候,就真正做到了等待多个请求完成再执行下一个请求

因为上面是在项目中遇到的问题,所以只是粗略的写了一下,中午的时候写了一份Demo.(正好又学习了一下GitHub使用,原来从来没往上面提交过代码....).和上面的代码不一样.列举了三种不同的方式,助于大家分辨.有想看的可以去看看

你可能感兴趣的:(GCD在AFN中等待多个请求完成执行下个请求)