同步AF网络操作和解析



我被困在连续执行Afnetworking操作中。目前我可以得到所有操作的响应,但这些不是连续的。我需要把它连载。

我正在做的是

    for (int i = 0; i < myArray.count; i++) {
            //Creating Soap Request
            // Adding that SoapRequest to Operation
                AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
                [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // On success I am parsing the response
// Parsing is executed here..
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // Failure message
        }
// If Network is reachable then adding the Operation in queue
        [[PFAPIClient sharedClient] enqueueHTTPRequestOperation:operation];
        // Adding the operation (For multiple execution of operations) 
        }

因此,我得到的回复不是连续的。请帮帮我。那么,如何串行获取该响应并相应地解析该响应。。成功解析后,我必须显示它们。。

您可以将PFAPIClientNSOperationQueue中的maxConcurrentOperationCount设置为1,也可以使用依赖项,例如:

AFHTTPRequestOperation *previousOperation = nil;
for (int i = 0; i < myArray.count; i++) {
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:... failure:...];
    if (previousOperation) {
        [operation addDependency:previousOperation];
    }
    [[PFAPIClient sharedClient] enqueueHTTPRequestOperation:operation];
    previousOperation = operation;
}

你必须按顺序执行这些操作,这太糟糕了,因为你会付出巨大的性能代价,但如果你必须这样做,那有两种方法可以实现。

最新更新