2014-10-19 53 views
0

我困在連續執行Afnetworking操作。目前我能夠獲得所有的操作響應,但這些不是連續的。我需要使它串行。同步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) 

     } 

因此我得到的迴應是不連續的。請幫助我。 那麼如何連續獲得此響應,並相應地解析響應。經過成功解析我已經顯示他們..

回答

0

您可以設置PFAPIClient的的maxConcurrentOperationCountNSOperationQueue1或者你可以使用相關性,如:

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; 
} 

這太糟糕了,你必須按順序執行這些操作,因爲你會付出巨大的性能損失,但是如果你必須這樣做,那麼有兩種方法可以完成它。

+0

我已經添加了操作隊列中的所有操作並將最大併發操作數設置爲1.它對我有效.. @Rob感謝您的回覆! – iCoder 2014-10-21 05:06:38