gpt4 book ai didi

iphone - 这是从后台线程访问 UI 工具包的安全方法吗...?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:32 24 4
gpt4 key购买 nike

我必须从服务器下载一个图像,但我不想创建 NSURLConnection,我知道 UIKit 不是线程安全的,所以我尝试了这个,只需要确认这是安全的还是会导致崩溃(截至目前它工作正常。)我尝试了以下

看开关的情况2。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
{
CreateNewSurveyViewController *vc=[[CreateNewSurveyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
break;
}
case 1:{
MySurveyViewController *mySurveyViewController=[[MySurveyViewController alloc] init];
[self.navigationController pushViewController:mySurveyViewController animated:YES];
[mySurveyViewController release];
break;
}
case 2:{

self.progressHud.hidden = NO;
[self performSelectorInBackground:@selector(loadProfileImage) withObject:nil];
break;
}
default:
break;
}
}
-(void)loadProfileImage {

NSData* profileImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.userAccount.profileImageURL]];
[self performSelectorOnMainThread:@selector(launchSettingsView:) withObject:profileImageData waitUntilDone:YES];

}

-(void)launchSettingsView:(NSData*)profileImageData {
self.userAccount.userImage = [UIImage imageWithData:profileImageData];
self.progressHud.hidden = YES;
SettingsViewController* settingsViewController=[[SettingsViewController alloc] init];
settingsViewController.userAccount = self.userAccount;
settingsViewController.delegate = self;
[self.navigationController pushViewController:settingsViewController animated:YES];
[settingsViewController release];
}

最佳答案

这对我来说看起来很安全。您在后台线程上完成所有网络连接,然后仅在您回调的主线程方法中触摸 UI。通常人们会使用 GCD 或 NSOperationQueue,但这也应该有效。

关于iphone - 这是从后台线程访问 UI 工具包的安全方法吗...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12667760/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com