这是以异步方式发送多个NSMutableURLRequest的正确方法吗?

时间:2013-12-24 05:37:49

标签: ios iphone ipad asynchronous nsurlconnection

 __block   NSInteger outstandingRequests = [requestsArray count];
        for (NSURLRequest *request in requestsArray)
        {
            [NSURLConnection sendAsynchronousRequest:request
                                               queue:[NSOperationQueue mainQueue]
                                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
                                    {
                                      [self parseData:data];

                                       outstandingRequests--;
                                       if (outstandingRequests == 0)
                                       {
                                           [delegate finishApproveRejectAll];
                                       }
                                   }];
        }

2 个答案:

答案 0 :(得分:2)

如果请求一次多于一个,您应该使用NSOperationQueue

答案 1 :(得分:0)

把它放在.h

@property(nonatomic,retain)ASINetworkQueue *networkQueue;

设置请求

for (int i=0; i<[RequesttList count]; i++)
{
 ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
        [request setShouldContinueWhenAppEntersBackground:YES];
        request.tag = i;

        [[self networkQueue] addOperation:request];
}