Skip to content

Instantly share code, notes, and snippets.

@ericflo
Created October 19, 2011 07:43
Show Gist options
  • Select an option

  • Save ericflo/1297693 to your computer and use it in GitHub Desktop.

Select an option

Save ericflo/1297693 to your computer and use it in GitHub Desktop.
//
// NSURLConnection+Blocks.h
// SteveHolt
//
// Created by Eric Florenzano on 10/19/11.
// Copyright (c) 2011 Boilerplate Inc. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSURLConnection (Blocks)
#pragma mark Class API Extensions
+ (void)asyncRequest:(NSURLRequest *)request
success:(void(^)(NSData *, NSURLResponse *))successBlock_
failure:(void(^)(NSData *, NSError *))failureBlock_;
@end
//
// NSURLConnection+Blocks.m
// SteveHolt
//
// Created by Eric Florenzano on 10/19/11.
// Copyright (c) 2011 Boilerplate Inc. All rights reserved.
//
#import "NSURLConnection+Blocks.h"
@implementation NSURLConnection (Blocks)
+ (void)asyncRequest:(NSURLRequest *)request
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) {
dispatch_async(dispatch_get_main_queue(), ^{
failureBlock_(data, error);
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
successBlock_(data, response);
});
}
[pool release];
});
}
@end
@0xced

0xced commented Oct 19, 2011

Copy link
Copy Markdown

You must test if (!data) instead of if (error)

@ericflo

ericflo commented Oct 20, 2011 via email

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment