0

I’m sorry if this question is too basic, but I can’t seem to find a an answer online.

I want to fetch the JSON result and have them returned with the class method below. But as you can see, by fetching the JSON in the block method, I don’t have a way to return them as result.

What is the correct way to to return them as NSDictionary from inside block method, or is there any other way to simplify this?

+ (NSDictionary *) fetchtPostsCount:(NSString *) count
                              page: (NSString *) page  {
  NSDictionary *requestParameter = [[NSDictionary alloc] initWithObjectsAndKeys:count, @"count", page, @"page", nil];

  [[self sharedClient] GET:@"get_recent_posts"
                parameters:requestParameter success:^(NSURLSessionDataTask *task, id responseObject) {
                  NSLog(@"%@", [responseObject objectForKey:@"posts"]);

                } failure:^(NSURLSessionDataTask *task, NSError *error) {
                  NSLog(@"%@", error);
                }];


  return nil;
}
sayzlim
  • 275
  • 1
  • 2
  • 16
  • 1
    possible duplicate of [Returning method object from inside block](http://stackoverflow.com/questions/22267865/returning-method-object-from-inside-block) – Wain Apr 25 '14 at 17:07
  • @Wain thanks, it’s exactly what I’m looking for. – sayzlim Apr 26 '14 at 06:05

1 Answers1

1

AFNetworking executes requests on different thread, and calls the success or failure block when its done. Conceptually, you can imagine that your fetchPostsCount method will have already completed and returned its value by the time request is finished.

You almost certainly want it to work that way. Running the request on another thread and NOT waiting for it, allows your main UI thread to continue processing events and rendering screen updates. You don't want to get in the way of those things, or the user (and iOS) will get unhappy.

However, if you insist on waiting for the request to complete before returning, you could set a flag to monitor the status of the request, and then wait on that flag until the request is complete:

BOOL requestComplete = NO;
id requestResponseObject = nil;
[[self sharedClient] GET:@"get_recent_posts"
            parameters:requestParameter success:^(NSURLSessionDataTask *task, id responseObject) {
              requestResponseObject = responseObject;
              requestComplete = YES;
              NSLog(@"%@", [responseObject objectForKey:@"posts"]);

            } failure:^(NSURLSessionDataTask *task, NSError *error) {
              requestComplete = YES;
              NSLog(@"%@", error);
            }];
while (!requestComplete)
{
    // Tie up the thread, doing nothing...
}
// Proceed
adpalumbo
  • 3,031
  • 12
  • 12
  • Is there any recommended way to fetch the result, parse them into appropriate format, and store them to my chosen storage asynchronously? I want to keep the request simple by having a class method wrapping the request parameters. I think I should read more about AFNetworking (just saw something like operation start, etc). But this answer set me to right direction, thanks again. – sayzlim Apr 25 '14 at 18:00