Isa*_*c A 2 multithreading objective-c uiviewcontroller ios
我的应用程序目前由a TableViewController和a组成ViewController.当选中a cell时TableView,ViewController会推送,这是主应用程序.此视图控制器先前将其全部加载UIViews到主线程中,这导致屏幕在代码运行时冻结,通常会导致用户认为它已崩溃.为了防止出现此问题并改善用户体验,我将代码更改为以下整体格式:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initialiseApp];
}
- (void) initialiseApp {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Initialising views
imageView = [[UIImageView alloc] initWithImage:[self getImageFromUrl:currentWallpaperURL]];
[imageView setFrame:CGRectMake(0.0, 100, screenWidth, (screenWidth/7)*4)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
// etc etc for other views
dispatch_async(dispatch_get_main_queue(), ^{
//Add subviews to UI
[[self view] addSubview:imageView];
});
});
}
Run Code Online (Sandbox Code Playgroud)
当应用程序在模拟器中运行时,这会导致ViewController加载为空白屏幕,稍后加载UI一段时间后.在加载过程中,我会在屏幕上显示某种形式的微调器或文本.因此,我想澄清一下这个主题:
正在加载应用程序UI时ViewController打开(或者,当应用程序启动时)常规?如果没有,什么是更好的替代方案,以防止应用程序在启动时冻结10秒?
谢谢.
我倾向于在后台线程上创建UI元素时遇到问题,所以如果可能的话我会避免这种情况(Apple也这么说).在您的情况下,不是在后台加载UI元素,而是在后台加载图像,然后在加载图像时创建UI元素.举个例子,
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImage *image = [self getImageFromUrl:currentWallpaperURL];
dispatch_async(dispatch_get_main_queue(), ^{
//Add subviews to UI
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setFrame:CGRectMake(0.0, 100, screenWidth, (screenWidth/7)*4)];
[imageView setContentMode:UIViewContentModeScaleAspectFit];
[[self view] addSubview:imageView];
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
814 次 |
| 最近记录: |