Created
April 5, 2012 15:04
Tumblr dashrboard request
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
OARequestParameter* likesParameter = [OARequestParameter requestParameter:@"likes" value:@"1"]; | |
NSURL* URL = [NSURL URLWithString:dashboardURL]; | |
__block void (^successBlock)(AFHTTPRequestOperation *operation, id JSON); | |
__block void (^alternateSuccessBlock)(AFHTTPRequestOperation *operation, id JSON); | |
if (ID && block) { | |
__block int offset = 0; | |
__block NSMutableArray* allPosts = [NSMutableArray array]; | |
successBlock = ^(AFHTTPRequestOperation* operation, id JSON) { | |
NSArray* posts = [[JSON objectForKey:@"response"] objectForKey:@"posts"]; | |
for (NSDictionary* attributes in posts) { | |
if ([(NSNumber*)[attributes objectForKey:@"id"] isEqualToNumber:ID]) { | |
block(allPosts, YES); | |
return; | |
} | |
else { | |
[allPosts addObject:attributes]; | |
} | |
} | |
offset += 20; | |
NSLog(@"%i", offset); | |
NSArray* parameters = [NSArray arrayWithObjects:likesParameter, [OARequestParameter requestParameter:@"offset" value:[NSString stringWithFormat:@"%i", offset]], nil]; | |
NSMutableURLRequest* request = [self.user authenticationRequestWithURL:URL parameters:parameters method:HTTPMETHOD_GET]; | |
AFJSONRequestOperation* jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil]; | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
block(nil, NO); | |
}]; | |
[jsonOperation start]; | |
}; | |
alternateSuccessBlock = ^(AFHTTPRequestOperation* operation, id JSON) { | |
NSLog(@"OK"); | |
NSArray* posts = [[JSON objectForKey:@"response"] objectForKey:@"posts"]; | |
for (NSDictionary* attributes in posts) { | |
if ([(NSNumber*)[attributes objectForKey:@"id"] isEqualToNumber:ID]) { | |
block(allPosts, YES); | |
return; | |
} | |
else { | |
[allPosts addObject:attributes]; | |
} | |
} | |
offset += 20; | |
NSLog(@"%i", offset); | |
NSArray* parameters = [NSArray arrayWithObjects:likesParameter, [OARequestParameter requestParameter:@"offset" value:[NSString stringWithFormat:@"%i", offset]], nil]; | |
NSMutableURLRequest* request = [self.user authenticationRequestWithURL:URL parameters:parameters method:HTTPMETHOD_GET]; | |
AFJSONRequestOperation* jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil]; | |
[operation setCompletionBlockWithSuccess:successBlock failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
block(nil, NO); | |
}]; | |
[jsonOperation start]; | |
}; | |
} | |
NSMutableURLRequest* request = [self.user authenticationRequestWithURL:URL parameters:[NSArray arrayWithObject:likesParameter] method:HTTPMETHOD_GET]; | |
AFJSONRequestOperation* operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:nil failure:nil]; | |
if (block) { | |
if (ID) { | |
[operation setCompletionBlockWithSuccess:successBlock failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
block(nil, NO); | |
}]; | |
} | |
else { | |
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id JSON) { | |
block([[JSON objectForKey:@"response"] objectForKey:@"posts"], YES); | |
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { | |
block(nil, NO); | |
}]; | |
} | |
} | |
[self.operationQueue addOperation:operation]; |
L#36 & L#63: Yeah, an operation started here will be short-lived. Try adding these to the operation queue you use in L#85.
Just throwing something else out there: have you tried copy
-ing block
for use in the block contexts?
If you do a simple log in the completion of each of these requests, do you see that they're actually executing?
You may also gain some clarity into making this work by refactoring. Rather than instantiating a shared success block, why not organize everything into a more traditional recursive method?
- (void)fetchPostsWithOffset:(NSUInteger)offset success:(void (^)(NSArray *posts))success;
Where in addition to executing the success block, the completion of this operation conditional calls fetchPostsWithOffset:success:
again with a new offset.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"OK" is never printed because the alternateSuccessBlock is never called. I suppose that the operation finishes properly though.