Created
May 31, 2009 05:01
-
-
Save cbarrett/120769 to your computer and use it in GitHub Desktop.
A sketch for an implementation of a better NSURLConnection, NiceURLConnection
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
// by Colin Barrett (colin at springs and struts dot com) | |
// I release this sketch to the community under the MIT License. | |
// No really this is just a sketch I haven't even compiled it or anything. | |
// Other ides: | |
// - Failure & success completion selectors ? | |
// - Single delegate method called on both success and failure. | |
@interface NiceURLConnection : NSURLConnection { | |
@private | |
id delegate; | |
NSData *data; | |
NSURLResponse *response; | |
NSError *error; | |
BOOL finished; | |
} | |
@property (readonly) NSData *data; | |
@property (readonly) NSURLResponse *response; | |
@property (readonly) NSError *error; | |
@property (readonly, getter=isFinished) BOOL finished; | |
@end | |
// ------- | |
@interface NiceURLConnection () | |
@property (readwrite, getter=isFinished) BOOL finished; | |
@end | |
@implementation NiceURLConnection | |
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately | |
{ | |
if (self = [super initWithRequest:request delegate:self startImmediately:startImmediately]) { | |
delegate = delegate; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[data release]; data = nil; | |
[response release]; response = nil; | |
[error release]; error = nil; | |
[super dealloc]; | |
} | |
@synthesize data; | |
@synthesize response; | |
@synthesize error; | |
@synthesize finished; | |
#pragma mark - | |
#pragma mark Delegate | |
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)inResponse | |
{ | |
[data release]; | |
data = [[NSMutableData alloc] init]; | |
[response release]; | |
response = [inResponse retain]; | |
if (delegate && [delegate respondsToSelector:@selector(connection:didReceiveResponse:)]) { | |
[delegate connection:connection didReceiveResponse:inResponse]; | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)inData | |
{ | |
[data appendData:inData]; | |
if (delegate && [delegate respondsToSelector:@selector(connection:didReceiveData:)]) { | |
[delegate connection:connection didReceiveData:inData]; | |
} | |
} | |
- (void)connectionDidFinishLoading:(NSURLConnection *)connection | |
{ | |
[self setFinished:YES]; | |
if (delegate && [delegate respondsToSelector:@selector(connectionDidFinishLoading:)]) { | |
[delegate connectionDidFinishLoading:connection]; | |
} | |
} | |
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)inError | |
{ | |
[self setFinished:YES]; | |
error = [inError copy]; | |
if (delegate && [delegate respondsToSelector:@selector(connection:didFailWithError:)]) { | |
[delegate connection:connection didFailWithError:inError]; | |
} | |
} | |
// XXX forward all remaining delegate methods with forwardInvocation or just paste them in. | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment