0

我正在学习 Objective-C 并试图更好地理解 GCD。我创建了一个APICaller进行 API 调用的对象 ( ),然后向其委托提供信息。在这个对象的委托的 ( TableViewControllerA)viewDidLoad方法中,我调用APICaller' 的方法之一,然后使用该信息更新detailTextLabel.text两个静态单元格的 。我的问题:为什么,当我使用 时dispatch_asyncdetailTextLabel.text更新比没有它时快得多?

这会更新单元格,但延迟很长:

- (void)viewDidLoad
{
  APICaller *apiCaller = [APICaller alloc] init];

  [apiCaller getInformationWithArgument:self.argument completionHandler:^(NSString  *results, NSError *error) {
    _staticCell.detailTextLabel.text = results;
  }

}

...虽然这会立即更新单元格:

- (void)viewDidLoad
{
  APICaller *apiCaller = [APICaller alloc] init];

  [apiCaller getInformationWithArgument:self.argument completionHandler:^(NSString  *results, NSError *error) {
    dispatch_async(dispatch_get_main_queue, ^(void) {
           _staticCell.detailTextLabel.text = results;
      });
  }

}
4

1 回答 1

1

第一个代码片段中显示的完成处理程序没有在主线程上运行,因此只要系统决定需要更新,就会更新。第二个片段使用 GCD 在主线程上显式运行,因此会立即更新。

于 2014-10-30T14:27:47.157 回答