Skip to content

Commit

Permalink
Add blocks-based method to UAGithubURLConnection.
Browse files Browse the repository at this point in the history
+ (void)asyncRequest:(NSURLRequest *)request requestType:(UAGithubRequestType)reqType responseType:(UAGithubResponseType)respTyp success:(void(^)(NSData *, NSURLResponse *))successBlock_ failure:(void(^)(NSData *, NSError *))failureBlock_;

Takes a successBlock and a failureBlock, removing the need for delegate callbacks etc. Influenced by https://github.com/rickerbh/NSURLConnection-Blocks.
  • Loading branch information
Owain R Hunt committed Oct 24, 2011
1 parent 544395c commit df24e26
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions UAGithubURLConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
@property (nonatomic, retain) NSString *identifier;

- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate requestType:(UAGithubRequestType)reqType responseType:(UAGithubResponseType)respType;
+ (void)asyncRequest:(NSURLRequest *)request requestType:(UAGithubRequestType)reqType responseType:(UAGithubResponseType)respTyp success:(void(^)(NSData *, NSURLResponse *))successBlock_ failure:(void(^)(NSData *, NSError *))failureBlock_;

- (void)resetDataLength;
- (void)appendData:(NSData *)newData;

Expand Down
21 changes: 21 additions & 0 deletions UAGithubURLConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate requestType:
return self;
}


+ (void)asyncRequest:(NSURLRequest *)request requestType:(UAGithubRequestType)reqType responseType:(UAGithubResponseType)respType success:(void(^)(NSData *, NSURLResponse *))successBlock_ failure:(void(^)(NSData *, NSError *))failureBlock_
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if (error) {
failureBlock_(data,error);
} else {
successBlock_(data,response);
}

[pool release];
});
}


- (void)resetDataLength
{
[data setLength:0];
Expand Down

0 comments on commit df24e26

Please sign in to comment.