使用GCD刷新UI

时间:2017-12-05 15:22:32

标签: ios objective-c user-interface refresh grand-central-dispatch

我有一个在iOS编码中困扰我的问题。首先我展示代码:

[AlinkApi openalinkDevicesGetbyUser:^(AlinkResponse *response) {
    [_delegate sideHomeViewItemClick:self.deviceArray[self.selectedIndex]];
    NSIndexPath *selIndex = [NSIndexPath indexPathForRow:self.selectedIndex inSection:0];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.deviceTableView selectRowAtIndexPath:selIndex animated:YES scrollPosition:UITableViewScrollPositionTop];
    });
} sucessBlock:^{
} failBlock:^{
}];

我需要在tableView中选择一行,我应该在主队列中执行[self.deviceTableView selectRowAtIndexPath:selIndex animated:YES scrollPosition:UITableViewScrollPositionTop];吗?并且代码有问题吗?我的项目有时会崩溃,没有任何更多的信息,只有提示是EXC_BAD_ACCESS,我尝试了很多方法无法找出原因,但有人告诉我原因可能是不能刷新主队列中的UI。

1 个答案:

答案 0 :(得分:0)

是的,每次与UIKit交互时都应该使用主线程。

我也关注所有来自后台线程的self属性的所有其他引用。您不应该从多个线程访问属性,除非(a)您知道该对象是线程安全的;或者(b)你已经实现了一些同步机制,使自己的线程安全。