Im trying to understand some code I found on the internet. Im trying to tweak it so I can use it in my own program. In my program Ive made this a instance method of a singleton. I understand most of what this is doing but dont get the “block” part. What is the block for? and in my implementation, what should I pass as the parameter in place of NSSet Photos. I dont understand this since, Im actually hoping to “get” photos from the server for that location. So what am I sending ?
+ (void)photosNearLocation:(CLLocation *)location
block:(void (^)(NSSet *photos, NSError *error))block
{
NSLog(@"photosNearLocation - Photo.m");
NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
[mutableParameters setObject:[NSNumber
numberWithDouble:location.coordinate.latitude] forKey:@"lat"];
[mutableParameters setObject:[NSNumber
numberWithDouble:location.coordinate.longitude] forKey:@"lng"];
[[GeoPhotoAPIClient sharedClient] getPath:@"/photos"
parameters:mutableParameters
success:^(AFHTTPRequestOperation *operation, id JSON)
{
NSMutableSet *mutablePhotos = [NSMutableSet set];
NSLog(@" Json value received is : %@ ",[JSON description]);
for (NSDictionary *attributes in [JSON valueForKeyPath:@"photos"])
{
Photo *photo = [[Photo alloc]
initWithAttributes:attributes];
[mutablePhotos addObject:photo];
}
if (block) {
block([NSSet setWithSet:mutablePhotos], nil);
}
}failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
if (block)
{
block(nil, error);
NSLog(@"Error in Photo.m line 145 %@ ", [error description]);
}
}];
}
No need to pass anything for the photo set. It’s a parameter to the block. The caller’s job is to pass a block that will be invoked when the method finishes some asynchronous work. So you’d call it like this: