Return Result of Completion Block

孤街醉人 提交于 2019-12-28 07:03:54

问题


So I'm trying to build a layer on top of the Twitter API (among others) for a project and I need to find a way to return the result of the Twitter actions to the layer of abstraction.

Right now my setup is something like this, for example:

-(NSDictionary *)sendTweet:(Tweet *)tweet {
        __block NSMutableDictionary *responseDictionary;

    NSLog(@"Sending tweet");

    NSMutableDictionary *twitterRequestDictionary = [[NSMutableDictionary alloc] init];
    [twitterRequestDictionary setObject:tweet.tweetBody forKey:@"status"];

    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"]
                                             parameters:twitterRequestDictionary
                                          requestMethod:TWRequestMethodPOST];
    [request setAccount:self.userAccount];


    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        responseDictionary  = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"Response dictionary: %@", responseDictionary);
        return responseDictionary;
    }];

}

But because the 'performRequestWithHandler:' method returns 'void' I the final line causes an error.

I've also tried placing the 'return' statement outside of the block and locking the execution of the block section of code after discovering this article: http://omegadelta.net/2011/05/10/how-to-wait-for-ios-methods-with-completion-blocks-to-finish

Still no luck.

I hope someone can shed some light on this way to do it (or perhaps suggest a better method to return the data).


回答1:


Why don't you also use a block to return the response? Something like:

-(void)sendTweet:(Tweet *)tweet withResponseCallback:(void (^)(NSMutableDictionary *responseDictionary))callback {

    NSLog(@"Sending tweet");

    NSMutableDictionary *twitterRequestDictionary = [[NSMutableDictionary alloc] init];
    [twitterRequestDictionary setObject:tweet.tweetBody forKey:@"status"];

    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"]
                                             parameters:twitterRequestDictionary
                                          requestMethod:TWRequestMethodPOST];
    [request setAccount:self.userAccount];


    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSMutableDictionary *responseDictionary  = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"Response dictionary: %@", responseDictionary);
        callback(responseDictionary);
    }];
}



回答2:


Since you are using the asynchronous method it is difficult to say when your method will return the data. So you can consider other options to return the result. For example it might be useful to post a notification, send a message, set some property or even show an alert view.

As for the article' code sample I would try something like the following

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    NSData *data = [self loadDataWithConditionLock];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self updateUIWithData:data];
    });
});


来源:https://stackoverflow.com/questions/12095784/return-result-of-completion-block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!