Permalink
Browse files

Experimental implementation using a single, dedicated network thread,…

… rather than spinning up new run loops for each request
  • Loading branch information...
1 parent 450ff56 commit 5349a1d4ffd3f612f0cbc94beb8eb55d97e7b9e3 @mattt mattt committed Aug 23, 2011
Showing with 26 additions and 17 deletions.
  1. +26 −17 AFNetworking/AFHTTPRequestOperation.m
@@ -85,7 +85,6 @@ @interface AFHTTPRequestOperation ()
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion;
- (id)initWithRequest:(NSURLRequest *)urlRequest;
-- (void)cleanup;
@end
@implementation AFHTTPRequestOperation
@@ -102,6 +101,25 @@ @implementation AFHTTPRequestOperation
@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 @@ - (void)dealloc {
[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 @@ - (void)setState:(AFHTTPOperationState)state {
case AFHTTPOperationFinishedState:
[[AFNetworkActivityIndicatorManager sharedManager] stopAnimating];
[[NSNotificationCenter defaultCenter] postNotificationName:AFHTTPOperationDidFinishNotification object:self];
- [self cleanup];
break;
default:
break;
@@ -233,7 +241,11 @@ - (void)start {
}
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 @@ - (void)start {
}
[self.connection start];
-
- [runLoop run];
+
}
- (void)cancel {
self.isCancelled = YES;
- [self.connection cancel];
-
- [self cleanup];
+ [self.connection cancel];
}
#pragma mark - AFHTTPRequestOperation

0 comments on commit 5349a1d

Please sign in to comment.