Tuesday, 13 August 2013

AFNetworking nested calll

AFNetworking nested calll

Right now, I got a GET request, after it's finished, I got the json back,
and then I want to use the id from the json to execute another fetch
request. It's like a nested fetch request right after another. For
example:
+ (void)searchPhotoWithTags:(NSArray *)tags page:(NSInteger)page
perPage:(NSInteger)perPage completionBlock:(void (^)(NSArray *, NSError
*))block
{
NSDictionary *dict = @{@"method": @"search", @"api_key": kAppKey,
@"tags": [tags componentsJoinedByString:@","], @"per_page": [NSString
stringWithFormat:@"%d", perPage], @"page": [NSString
stringWithFormat:@"%d", page], @"format": @"json"};
[[LDHttpClient sharedClient] getPath:@"" parameters:dict
success:^(AFHTTPRequestOperation *operation, id responseObject) {
[[responseObject valueForKeyPath:@"photos.photo"]
enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Photo *photo = [[Photo alloc] initWithPhotoId:[NSNumber
numberWithInt:[obj[@"id"] integerValue]]
andSecret:obj[@"secret"]];
//down below, I want to use photo.photoId to execute another
request but the data is not completed. what's the better way
to do this?
[PhotoSize getPhotoSizesWithPhotoId:photo.photoId
completionBlock:^(NSArray *photoSizes, NSError *error) {
[photos addObject:@{@"photo": photo, @"sizes": photoSizes}];
}];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}

No comments:

Post a Comment