Skip to content

Instantly share code, notes, and snippets.

@lbrndnr
Created April 5, 2012 15:04
Show Gist options
  • Save lbrndnr/2311820 to your computer and use it in GitHub Desktop.
Save lbrndnr/2311820 to your computer and use it in GitHub Desktop.
Tumblr dashrboard request
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];
@lbrndnr
Copy link
Author

lbrndnr commented Apr 5, 2012

"OK" is never printed because the alternateSuccessBlock is never called. I suppose that the operation finishes properly though.

@mattt
Copy link

mattt commented Apr 5, 2012

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.

@mattt
Copy link

mattt commented Apr 5, 2012

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