如何从完成块中获取变量?

时间:2013-08-24 21:48:48

标签: ios objective-c multithreading macos

  • (无效)fetchLastMessageInChannel

{           __weak id weakSelf = self;

    for (ANKChannel *channel in self.channelArray)
    {
            NSLog(@"channels %@",channel);

             NSLog(@"channel last message %@",channel.latestMessageID);

        [[ClientManager currentClient] fetchMessageWithID:channel.latestMessageID inChannel:channel
                                               completion:^(id responseObject, ANKAPIResponseMeta *meta, NSError *error)
         {
             NSLog(@"message object %@",responseObject);
             ANKMessage *message = responseObject;

             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf populateTextViews:message.text];
             });

             NSLog(@"message text %@",message.text);

         }];

    }

}

- (void)populateTextViews:(NSString *)消息 {

NSMutableArray *textViews = [@[] mutableCopy];

NSMutableAttributedString *postText = [[NSMutableAttributedString alloc] initWithString:message];
[postText addAttributes:@{
                          NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody],
                          NSForegroundColorAttributeName : [UIColor darkTextColor]
                          }
                  range:NSMakeRange(0, postText.length)];

UITextView *postTextView = [[UITextView alloc] initWithFrame:CGRectMake(80, 30, kPostLabelMaxWidth, 44)];
postTextView.attributedText = postText;
postTextView.dataDetectorTypes = UIDataDetectorTypeAll;
postTextView.backgroundColor = [UIColor whiteColor];
postTextView.editable = NO;
postTextView.scrollEnabled = NO;
postTextView.clipsToBounds = NO; // So it doesn't clip the text selector

CGRect textViewBounds = postTextView.bounds;
textViewBounds.origin = CGPointMake(80, 30);
textViewBounds.size.width = MAX(textViewBounds.size.width, kPostLabelMaxWidth);
textViewBounds.size.height = postTextView.contentSize.height;


postTextView.bounds = textViewBounds;

[postTextView sizeToFit]; // Reload the content size

[textViews addObject:postTextView];



self.channelTextViewArray = [textViews copy];

}

这就是我现在的立场,只要我的方法与我收到的帮助一致。 self.channelTextViewArray返回nil并导致崩溃,因为populateTextViews(NSString *)消息永远不会被调用。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果ClientManager调用是异步的,populateTextViews方法将在异步调用返回之前完成,这就是您无法使用其完成块中设置的值的原因。

要么......

NSMutableAttributedString *postText = [[NSMutableAttributedString alloc] initWithString:messageText];

...在完成块内部,或者在拥有messageText后调用完成块内的方法。在这样做时,您不必声明__block变量。

如果有UI更新,请确保在主线程上发生。

修改

这是基本的想法,但我猜你正在更新多个文本视图,所以你可能需要更改周围的签名。如果你有了基本的想法 - 调用异步方法不会中断你的代码流(基本上它说“当你有机会时,可能在另一个线程上执行此操作”)。这就是你有一个完成块的原因 - 它是你代码中的一个地方,你知道你调用的异步方法已经完成。

如果块中的内容根本没有被调用,请确保self.channelArray具有值,并查看fetchMessageWithID出现问题时的作用。

- (void)populateTextViews 
{
    __weak id weakSelf = self;
    for (ANKChannel *channel in self.channelArray)
    {    
        [[ClientManager currentClient] fetchMessageWithID:channel.latestMessageID inChannel:channel
                                               completion:^(id responseObject, ANKAPIResponseMeta *meta, NSError *error)
         {
             NSLog(@"message object %@",responseObject);
             ANKMessage *message = responseObject;
             dispatch_async(dispatch_get_main_queue(), ^{
                 [weakSelf updateTextView:message.text];
             });        
         }];
    }
}

- (void)updateTextView:(NSString *)message
{
    // Make an attributed string from the post text content
    NSMutableAttributedString *postText = [[NSMutableAttributedString alloc] initWithString:messageText];
    self.textView.attributedText = postText;
}