【问题标题】:Return Result of Completion Block完成块返回结果
【发布时间】:2012-08-19 05:08:26
【问题描述】:

所以我试图在 Twitter API(以及其他)之上为一个项目构建一个层,我需要找到一种方法将 Twitter 操作的结果返回到抽象层。

现在我的设置是这样的,例如:

-(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;
    }];

}

但是因为 'performRequestWithHandler:' 方法返回 'void' 我最后一行导致错误。

在发现这篇文章后,我还尝试将'return'语句放在块之外并锁定代码块部分的执行:http://omegadelta.net/2011/05/10/how-to-wait-for-ios-methods-with-completion-blocks-to-finish

还是没有运气。

我希望有人可以对这种方式有所了解(或者建议一种更好的方法来返回数据)。

【问题讨论】:

    标签: objective-c ios twitter objective-c-blocks


    【解决方案1】:

    为什么不也使用块来返回响应?比如:

    -(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);
        }];
    }
    

    【讨论】:

    • 是的,这看起来是一个好的开始。有没有办法返回数据呢?就像,也许等到完成处理程序被调用然后返回它?这让我发疯了!
    • 您不希望sendTweet: 返回响应。否则,它将在 Twitter 请求期间阻止 UI,这可能是在有损连接上的几分钟。您需要查看如何使用响应。一种常见的模式是让回调块显示成功/错误消息,并且可能仅在发布成功时清除带有推文正文的文本视图。
    【解决方案2】:

    由于您使用的是异步方法,因此很难说您的方法何时会返回数据。因此,您可以考虑其他选项来返回结果。例如,发布通知、发送消息、设置某些属性甚至显示警报视图可能很有用。

    至于文章的代码示例,我会尝试以下内容

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-13
      • 2014-07-22
      • 2013-02-10
      相关资源
      最近更新 更多