From 5349a1d4ffd3f612f0cbc94beb8eb55d97e7b9e3 Mon Sep 17 00:00:00 2001 From: Mattt Thompson Date: Mon, 22 Aug 2011 19:16:31 -0500 Subject: [PATCH] Experimental implementation using a single, dedicated network thread, rather than spinning up new run loops for each request --- AFNetworking/AFHTTPRequestOperation.m | 43 ++++++++++++++++----------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/AFNetworking/AFHTTPRequestOperation.m b/AFNetworking/AFHTTPRequestOperation.m index bc3ca48..825706c 100644 --- a/AFNetworking/AFHTTPRequestOperation.m +++ b/AFNetworking/AFHTTPRequestOperation.m @@ -85,7 +85,6 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr @property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion; - (id)initWithRequest:(NSURLRequest *)urlRequest; -- (void)cleanup; @end @implementation AFHTTPRequestOperation @@ -102,6 +101,25 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr @synthesize progress = _progress; @synthesize completion = _completion; +static NSThread *_networkRequestThread = nil; + ++ (NSThread *)networkRequestThread { + if (!_networkRequestThread) { + _networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; + [_networkRequestThread start]; + } + + return _networkRequestThread; +} + ++ (void)networkRequestThreadEntryPoint:(id)object { + do { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + [[NSRunLoop currentRunLoop] run]; + [pool drain]; + } while (YES); +} + + (id)operationWithRequest:(NSURLRequest *)urlRequest completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion { @@ -163,15 +181,6 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr [super dealloc]; } -- (void)cleanup { - [self.outputStream close]; - for (NSString *runLoopMode in self.runLoopModes) { - [self.connection unscheduleFromRunLoop:[NSRunLoop currentRunLoop] forMode:runLoopMode]; - [self.outputStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:runLoopMode]; - } - CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]); -} - - (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block { self.progress = block; } @@ -198,7 +207,6 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr case AFHTTPOperationFinishedState: [[AFNetworkActivityIndicatorManager sharedManager] stopAnimating]; [[NSNotificationCenter defaultCenter] postNotificationName:AFHTTPOperationDidFinishNotification object:self]; - [self cleanup]; break; default: break; @@ -233,7 +241,11 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr } self.state = AFHTTPOperationExecutingState; - + + [self performSelector:@selector(operationDidStart) onThread:[[self class] networkRequestThread] withObject:nil waitUntilDone:YES modes:[self.runLoopModes allObjects]]; +} + +- (void)operationDidStart { self.connection = [[[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO] autorelease]; NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; @@ -243,16 +255,13 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr } [self.connection start]; - - [runLoop run]; + } - (void)cancel { self.isCancelled = YES; - [self.connection cancel]; - - [self cleanup]; + [self.connection cancel]; } #pragma mark - AFHTTPRequestOperation