Permalink
Browse files

Add request method that takes a pointer to an error instead of a fail…

…ure block.
  • Loading branch information...
Owain R Hunt
Owain R Hunt committed Apr 13, 2012
1 parent 4a1153d commit f820b615f0924dc186f93bbe3785510b0a59c7f1
Showing with 25 additions and 0 deletions.
  1. +1 −0 UAGithubEngine/UAGithubURLConnection.h
  2. +24 −0 UAGithubEngine/UAGithubURLConnection.m
@@ -24,5 +24,6 @@
@property (nonatomic, strong) NSString *identifier;
+ (id)asyncRequest:(NSURLRequest *)request success:(id(^)(NSData *, NSURLResponse *))successBlock failure:(id(^)(NSError *))failureBlock_;
++ (id)asyncRequest:(NSURLRequest *)request success:(id(^)(NSData *, NSURLResponse *))successBlock error:(NSError **)error;
@end
@@ -37,4 +37,28 @@ + (id)asyncRequest:(NSURLRequest *)request success:(id(^)(NSData *, NSURLRespons
//});
}
++ (id)asyncRequest:(NSURLRequest *)request success:(id(^)(NSData *, NSURLResponse *))successBlock error:(NSError *__autoreleasing *)error
+{
+ // This has to be dispatch_sync rather than _async, otherwise our successBlock executes before the request is done and we're all bass-ackwards.
+ //dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
+
+ @autoreleasepool
+ {
+ NSLog(@"New %@ connection: %@", request.HTTPMethod, request);
+
+ NSURLResponse *response = nil;
+ NSError *connectionError = nil;
+ NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&connectionError];
+
+ if (connectionError) {
+ *error = connectionError;
+ return nil;
+ } else {
+ return successBlock(data,response);
+ }
+ }
+
+ //});
+}
+
@end

0 comments on commit f820b61

Please sign in to comment.