nsurlconnection处理了多less个urlRequests

我经历了很多网站后,我一次调用20个urlReqests我发现,4个urlRequsts处理后,下一个接受请求将遇到错误,是超时错误。当我打电话5个Web服务在一个循环中首先4个Web服务正在执行和第五得到超时error.How处理这种情况任何帮助将不胜感激

你可以使用NSOperationQueue来完成这些任务。 这里是示例代码 –

代码为RequestManager.h: –

#import <Foundation/Foundation.h> @protocol RequestManagerDelegate <NSObject> @optional - (void)requestForURL:(NSURL *)url tag:(NSString *)tag type:(NSString *)type didComplete:(NSData *)data; - (void)requestForURL:(NSURL *)url tag:(NSString *)tag type:(NSString *)type didFailWithError:(NSError *)error; @end @interface RequestManager : NSObject + (RequestManager *) instance; - (void)requestForURL:(NSURL *)url tag:(NSString *)tag delegate:(id<RequestManagerDelegate >)delegate; @end 

代码为RequestManager.m: –

 #import "RequestManager.h" @interface RequestManager () { NSOperationQueue *requestQueue; } @end @implementation RequestManager static RequestManager *singletonInstance = nil; - (id)init { if(self = [super init]) { requestQueue = [[NSOperationQueue alloc] init]; requestQueue.maxConcurrentOperationCount = 2;//Here you can select maximum concurrent operations } return self; } + (RequestManager *) instance { @synchronized(self) { if(!singletonInstance) { singletonInstance = [[RequestManager alloc] init]; } } return singletonInstance; } - (void)requestForURL:(NSURL *)url tag:(NSString *)tag delegate:(id<RequestManagerDelegate >)delegate { [requestQueue setSuspended:YES]; RequestOperation *newOperation = [[RequestOperation alloc] initWithURL:url tag:tag delegate:delegate]; newOperation.queuePriority = NSOperationQueuePriorityVeryHigh; [requestQueue addOperation:newOperation]; NSArray *operations = requestQueue.operations; long operationsCount = operations.count; RequestOperation *operation; NSOperationQueuePriority priority = NSOperationQueuePriorityVeryHigh; for(long i = (operationsCount - 1); i >= 0; i--) { operation = [operations objectAtIndex:i]; if((operation.isExecuting || operation.isCancelled || operation.isFinished) == NO) { [operation setQueuePriority:priority]; priority = [self nextPriorityLowerThan:priority]; } } [requestQueue setSuspended:NO]; } - (NSOperationQueuePriority)nextPriorityLowerThan:(NSOperationQueuePriority)priority { NSOperationQueuePriority lowerPriority = NSOperationQueuePriorityVeryLow; switch (priority) { case NSOperationQueuePriorityVeryHigh: lowerPriority = NSOperationQueuePriorityHigh; break; case NSOperationQueuePriorityHigh: lowerPriority = NSOperationQueuePriorityNormal; break; case NSOperationQueuePriorityNormal: lowerPriority = NSOperationQueuePriorityLow; break; case NSOperationQueuePriorityLow: lowerPriority = NSOperationQueuePriorityVeryLow; break; default: break; } return lowerPriority; } @end 

现在RequestOperation.h – >

 #import <Foundation/Foundation.h> @interface RequestOperation : NSOperation @property(readonly, copy) NSURL *url; @property(strong , readonly) NSString *tag; @property(strong , readonly) id<RequestManagerDelegate > *delagate; - (id)initWithURL:(NSURL *)url tag:(NSString *)tag delegate:(id<RequestManagerDelegate >) delegate; @end 

现在RequestOperation.m

 #import "RequestOperation.h" @interface RequestOperation () { NSURLConnection *connection; } @property (nonatomic) long long int expectedContentLength; @property (nonatomic, readwrite) NSError* error; @property (nonatomic) BOOL isExecuting; @property (nonatomic) BOOL isConcurrent; @property (nonatomic) BOOL isFinished; @end @implementation RequestOperation - (id)initWithURL:(NSURL *)url tag:(NSString *)tag delegate:(id<RequestManagerDelegate >) delegate{ if ((self=[super init])) { _url = url; _tag = tag; _delagate=delagate; } return self; } - (void)start { NSURLRequest* request = [NSURLRequest requestWithURL:_url]; //handle here for your request type (post or get) self.isExecuting = YES; self.isConcurrent = YES; self.isFinished = NO; [[NSOperationQueue mainQueue] addOperationWithBlock:^ { connection = [NSURLConnection connectionWithRequest:request delegate:self]; }]; } - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response { return request; } - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { } - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { } - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { return cachedResponse; } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { self.isExecuting = NO; self.isFinished = YES; } - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error { self.error = error; self.isExecuting = NO; self.isFinished = YES; } - (void)setIsExecuting:(BOOL)isExecuting { [self willChangeValueForKey:@"isExecuting"]; _isExecuting = isExecuting; [self didChangeValueForKey:@"isExecuting"]; } - (void)setIsFinished:(BOOL)isFinished { [self willChangeValueForKey:@"isFinished"]; _isFinished = isFinished; [self didChangeValueForKey:@"isFinished"]; } - (void)cancel { [super cancel]; [connection cancel]; self.isFinished = YES; self.isExecuting = NO; } @end 

我给你我的代码,这是我如何pipe理多个并发请求asynchrounously。 你可以使用这是非常有效的。 您可以使用RequestManager.h中的方法声明请求url,并可以使用委托来处理结果。