Created
August 23, 2012 18:25
-
-
Save enriquez/3439923 to your computer and use it in GitHub Desktop.
another NSURLConnection in an NSOperation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MENetworkOperation.h | |
// | |
// Created by Michael Enriquez on 8/21/12. | |
// | |
// | |
#import <Foundation/Foundation.h> | |
@interface MENetworkOperation : NSOperation <NSURLConnectionDelegate> | |
- (id)initWithRequest:(NSURLRequest *)request; | |
@property (strong, nonatomic) NSMutableData *responseData; | |
@property (assign, nonatomic) BOOL isExecuting; | |
@property (assign, nonatomic) BOOL isFinished; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MENetworkOperation.m | |
// | |
// Created by Michael Enriquez on 8/21/12. | |
// | |
// | |
#import "MENetworkOperation.h" | |
@interface MENetworkOperation() | |
- (void)startConnection; | |
@property (copy, nonatomic) NSURLRequest *request; | |
@end | |
@implementation MENetworkOperation | |
#pragma mark - Network Thread | |
+ (void)networkRequestThreadEntryPoint:(id)object { | |
do { | |
[[NSRunLoop currentRunLoop] run]; | |
} while (YES); | |
} | |
+ (NSThread *)networkRequestThread { | |
static NSThread *_networkRequestThread = nil; | |
static dispatch_once_t oncePredicate; | |
dispatch_once(&oncePredicate, ^{ | |
_networkRequestThread = [[NSThread alloc] initWithTarget:self selector:@selector(networkRequestThreadEntryPoint:) object:nil]; | |
_networkRequestThread.name = @"Network Request Thread"; | |
[_networkRequestThread start]; | |
}); | |
return _networkRequestThread; | |
} | |
#pragma mark - NSObject | |
- (id)initWithRequest:(NSURLRequest *)theRequest { | |
self = [super init]; | |
if (self) { | |
self.request = theRequest; | |
self.responseData = [NSMutableData data]; | |
} | |
return self; | |
} | |
#pragma mark - NSOperation | |
- (void)start { | |
if (self.isReady) { | |
self.isExecuting = YES; | |
if (self.isCancelled) { | |
self.isExecuting = NO; | |
self.isFinished = YES; | |
} else { | |
[self performSelector:@selector(startConnection) onThread:[self.class networkRequestThread] withObject:self waitUntilDone:NO]; | |
} | |
} | |
} | |
- (BOOL)isConcurrent { | |
return 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"]; | |
} | |
#pragma mark - NSURLConnectionDelegate | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { | |
if (self.isCancelled) { | |
[connection cancel]; | |
self.isExecuting = NO; | |
self.isFinished = YES; | |
} else { | |
[self.responseData setLength:0]; | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { | |
if (self.isCancelled) { | |
[connection cancel]; | |
self.isExecuting = NO; | |
self.isFinished = YES; | |
} else { | |
[self.responseData appendData:data]; | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { | |
[self cancel]; | |
self.isExecuting = NO; | |
self.isFinished = YES; | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection { | |
self.isExecuting = NO; | |
self.isFinished = YES; | |
} | |
#pragma mark - Private | |
- (void)startConnection { | |
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO]; | |
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; | |
[connection start]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment