Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 为什么GCD使该代码正常工作?_Ios_Objective C_Objective C Blocks_Grand Central Dispatch_Dispatch Async - Fatal编程技术网

Ios 为什么GCD使该代码正常工作?

Ios 为什么GCD使该代码正常工作?,ios,objective-c,objective-c-blocks,grand-central-dispatch,dispatch-async,Ios,Objective C,Objective C Blocks,Grand Central Dispatch,Dispatch Async,我正在学习Objective-C,并试图更好地理解GCD。我创建了一个对象(APICaller),它进行API调用,然后向其委托提供信息。在这个对象的委托(TableViewControllerA)viewDidLoad方法中,我调用了APICaller的一个方法,然后使用该信息更新两个静态单元格的detailtextlab.text。我的问题是:为什么当我使用dispatch\u async时,detailtextlab.text会比没有它更新得更快 这会更新单元格,但延迟时间较长: - (v

我正在学习Objective-C,并试图更好地理解GCD。我创建了一个对象(
APICaller
),它进行API调用,然后向其委托提供信息。在这个对象的委托(
TableViewControllerA
viewDidLoad
方法中,我调用了
APICaller
的一个方法,然后使用该信息更新两个静态单元格的
detailtextlab.text
。我的问题是:为什么当我使用
dispatch\u async
时,
detailtextlab.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;
      });
  }

}

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

简单。所有UI更新都必须在主线程上完成,并且完成处理程序不在主线程上。啊。很简单。谢谢