Returning NSDictionary from async code block? [dup

2020-06-06 07:31发布

问题:

This question already has answers here:
Closed 8 years ago.

Possible Duplicate:
returning UIImage from block

Hi I'm trying to return dictionary of json twitter data so i can use it in my application. How ever it is being called from a async block. I can not save it or return it any thoughts?

  -(NSDictionary *)TweetFetcher
    {

    TWRequest *request = [[TWRequest alloc] initWithURL:
                          [NSURL URLWithString: @"http://search.twitter.com/search.json?
    q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"] parameters:nil 
    requestMethod:TWRequestMethodGET];


    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse 
    *urlResponse, 
    NSError *error)
     {
         if ([urlResponse statusCode] == 200) 
         {
             NSError *error;        
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData 
             options:0 error:&error];


             //resultsArray return an array [of dicitionaries<tweets>];
             NSArray* resultsArray = [dict objectForKey:@"results"]; 
             for (NSDictionary* internalDict in resultsArray)

                 NSLog([NSString stringWithFormat:@"%@", [internalDict 
             objectForKey:@"from_user_name"]]);
        ----> return dict; // i need this dictionary of json twitter data
         }
         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
         }];
      }

Thnx in advance!

回答1:

I feel like I've written a ton of this async code lately.

- (void)tweetFetcherWithCompletion:(void(^)(NSDictionary *dict, NSError *error))completion
{
    NSURL *URL = [NSURL URLWithString:@"http://search.twitter.com/search.json?q=iOS%205&rpp=5&with_twitter_user_id=true&result_type=recent"];
    TWRequest *request = [[TWRequest alloc] initWithURL:URL parameters:nil requestMethod:TWRequestMethodGET];

    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        if ([urlResponse statusCode] == 200) {
            NSError *error;
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

            if (error) {
                completion(nil, error);
                return;
            }

            //resultsArray return an array [of dicitionaries<tweets>];
            NSArray* resultsArray = [dict objectForKey:@"results"]; 
            for (NSDictionary* internalDict in resultsArray)
                NSLog(@"%@", [internalDict objectForKey:@"from_user_name"]);

            completion(dict, nil);
        }
        else {
            NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
            completion(nil, error);
        }
    }];
}

So, instead of calling self.tweetDict = [self TweetFetcher];, you would call it this way.

[self tweetFetcherWithCompletion:^(NSDictionary *dict, NSError *error) {
    if (error) {
        // Handle Error Somehow
    }

    self.tweetDict = dict;
    // Everything else you need to do with the dictionary.
}];