Skip to content

Instantly share code, notes, and snippets.

@dmathewwws
Last active August 29, 2015 14:19
Show Gist options
  • Save dmathewwws/d430e8817de54f138e81 to your computer and use it in GitHub Desktop.
Save dmathewwws/d430e8817de54f138e81 to your computer and use it in GitHub Desktop.
Completion blocks
// How to create your own completion blocks
+ (void)responseFrom4sq:(CLLocation*)currentLocation limit:(NSString*)limit block:(void (^)(NSArray *locationDictionary, NSError *error))completionBlock{
NSString *apiString4aq= @"https://api.foursquare.com/v2/venues/search?ll=";
NSString *clientID = @"&client_id=x&client_secret=y";
NSString *version = @"&v=20121219";
NSMutableURLRequest *foursqRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%1.6f,%1.6f%@%@&limit=%@",apiString4aq,currentLocation.coordinate.latitude,currentLocation.coordinate.longitude,clientID,version,limit]]];
dispatch_queue_t foursqQueue;
foursqQueue = dispatch_queue_create("com.thePlayApp.foursqQueue", NULL);
dispatch_async(foursqQueue, ^{
NSURLResponse *response = nil;
NSError *error = nil;
NSError *error2 = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:foursqRequest
returningResponse:&response
error:&error];
if(!error){
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error2];
if (!error2) {
NSLog(@"4sq data retrieved successfully");
NSDictionary *dataDictionary4sq = [json objectForKey:@"response"];
NSArray *venueArray = [dataDictionary4sq objectForKey:@"venues"];
completionBlock(venueArray,error);
}else{
completionBlock([NSArray array] ,error);
NSLog(@"4sq data not retrieved successfully :(");
}
}else{
completionBlock([NSArray array] ,error);
NSLog(@"4sq data not retrieved successfully :(");
}
});
}
// How to deal with them
[PlayUtility responseFrom4sq:_currentLocation limit:@"1" block:^(NSArray *locationDictionary, NSError *error) {
if (!error){
NSLog(@"in PlayUtility responseFrom4sq completed");
dispatch_async(dispatch_get_main_queue(), ^(){
[self getVenueData4sq:locationDictionary index:0];
});
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment