Experimental implementation using a single, dedicated network thread, rather than spinning up new run loops for each request

This commit is contained in:
Mattt Thompson 2011-08-22 19:16:31 -05:00
parent 450ff56e54
commit 5349a1d4ff

View file

@ -85,7 +85,6 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
@property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion; @property (readwrite, nonatomic, copy) AFHTTPRequestOperationCompletionBlock completion;
- (id)initWithRequest:(NSURLRequest *)urlRequest; - (id)initWithRequest:(NSURLRequest *)urlRequest;
- (void)cleanup;
@end @end
@implementation AFHTTPRequestOperation @implementation AFHTTPRequestOperation
@ -102,6 +101,25 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
@synthesize progress = _progress; @synthesize progress = _progress;
@synthesize completion = _completion; @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 + (id)operationWithRequest:(NSURLRequest *)urlRequest
completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion completion:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSData *data, NSError *error))completion
{ {
@ -163,15 +181,6 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
[super 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 { - (void)setProgressBlock:(void (^)(NSUInteger totalBytesWritten, NSUInteger totalBytesExpectedToWrite))block {
self.progress = block; self.progress = block;
} }
@ -198,7 +207,6 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
case AFHTTPOperationFinishedState: case AFHTTPOperationFinishedState:
[[AFNetworkActivityIndicatorManager sharedManager] stopAnimating]; [[AFNetworkActivityIndicatorManager sharedManager] stopAnimating];
[[NSNotificationCenter defaultCenter] postNotificationName:AFHTTPOperationDidFinishNotification object:self]; [[NSNotificationCenter defaultCenter] postNotificationName:AFHTTPOperationDidFinishNotification object:self];
[self cleanup];
break; break;
default: default:
break; break;
@ -234,6 +242,10 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
self.state = AFHTTPOperationExecutingState; 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]; self.connection = [[[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO] autorelease];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop]; NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
@ -244,15 +256,12 @@ static inline BOOL AFHTTPOperationStateTransitionIsValid(AFHTTPOperationState fr
[self.connection start]; [self.connection start];
[runLoop run];
} }
- (void)cancel { - (void)cancel {
self.isCancelled = YES; self.isCancelled = YES;
[self.connection cancel]; [self.connection cancel];
[self cleanup];
} }
#pragma mark - AFHTTPRequestOperation #pragma mark - AFHTTPRequestOperation