Skip to content

Instantly share code, notes, and snippets.

@0xc010d
Created March 16, 2012 17:27
Show Gist options
  • Select an option

  • Save 0xc010d/2051282 to your computer and use it in GitHub Desktop.

Select an option

Save 0xc010d/2051282 to your computer and use it in GitHub Desktop.
NSURLConnection with dispatch_async
[self dispatch:^{
id response = [Performer perform:[NSURL URLWithFormat:kChannelsURL, [NSString currentCountryCode]] error:NULL];
}];
- (id)perform:(NSURL *)url data:(id)data error:(NSError **)error {
static NSString * const kXRegionHeaderField = @"X-Region";
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Assept"];
[request addValue:[UserDataKeeper sharedDataKeeper].countryCode forHTTPHeaderField:kXRegionHeaderField];
[request setHTTPMethod:data ? @"POST" : @"GET"];
if (data) {
[request setHTTPBody:[[data JSONRepresentation] dataUsingEncoding:NSUTF8StringEncoding]];
}
NSURLConnection *connection = [[[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO] autorelease];
[connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[connection start];
while (!done) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
// if (error != NULL && self.error) {
// *error = self.error;
// }
return self.response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment