Created
March 16, 2012 17:27
-
-
Save 0xc010d/2051282 to your computer and use it in GitHub Desktop.
NSURLConnection with dispatch_async
This file contains hidden or 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
| [self dispatch:^{ | |
| id response = [Performer perform:[NSURL URLWithFormat:kChannelsURL, [NSString currentCountryCode]] error:NULL]; | |
| }]; |
This file contains hidden or 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
| - (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