我已经阅读了我能找到的所有相关问题,但我仍然陷入困境,所以我希望有人能发现我的推理错误。
我正在尝试定期更新一些 UIView。为简单起见,我将代码简化为以下内容。摘要:在 viewDidLoad 中,我在一个新的后台线程上调用了一个方法。该方法调用主线程上的一个方法,该方法应该更新一些 UILabel。代码似乎工作正常:后台线程不是主线程,调用 UILabel 更新的方法在主线程上。在代码中:
在 viewDidLoad 中:
[self performSelectorInBackground:@selector(updateMeters) withObject:self];
这将创建一个新的后台线程。我的方法 updateMeters(为简单起见)现在看起来像这样:
if ([NSThread isMainThread]) { //this evaluates to FALSE, as it's supposed to
NSLog(@"Running on main, that's wrong!");
}
while (i < 10) {
[self performSelectorOnMainThread:@selector(updateUI) withObject:nil waitUntilDone:NO];
//The code below yields the same result
// dispatch_async(dispatch_get_main_queue(), ^{
// [self updateUI];
// });
[NSThread sleepForTimeInterval: 1.05];
++i;
}
最后,updateUI 就是这样做的:
if ([NSThread isMainThread]) { //Evaluates to TRUE; it's indeed on the main thread!
NSLog(@"main thread!");
} else {
NSLog(@"not main thread!");
}
NSLog(@"%f", someTimeDependentValue); //logs the value I want to update to the screen
label.text = [NSString stringWithFormat:@"%f", someTimeDependentValue]; //does not update
据我所知,这应该有效。但不幸的是,它没有......被注释掉dispatch_async()
的结果是一样的。