GCD在AFN中等待多個請求完成執行下個請求

有六個請求,前五個請求爲異步請求,而第六個請求須要拿到前五個請求的數據再執行. 原本這種請求按照書上寫的這種形式就能夠.如:html

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請求,此時再使用這種格式就不能夠了.會形成最後一個請求開始時拿到的沒有拿到前幾個請求完成以後的數據.此時就可使用這種方法了git

建立一個全局的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<AFMultipartFormData> 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使用,原來歷來沒往上面提交過代碼....).和上面的代碼不同.列舉了三種不一樣的方式,助於你們分辨.有想看的能夠去看看github

相關文章
相關標籤/搜索