UIStoryboard instantiateViewControllerWithIdentifier:我们可以在后台线程中调用它吗?

时间:2015-11-18 23:49:20

标签: ios crash storyboard grand-central-dispatch dispatch-async

我正在尝试在后台线程中调用故事板的instantiateViewControllerWithIdentifier。但我在主线程上提出它。

这样做有好办法吗?请给我你的建议。

我的代码是这样的。

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@“some” bundle:nil];
  UIViewController *viewController = [storyBoard instantiateViewControllerWithIdentifier:@“some”];
   dispatch_async(dispatch_get_main_queue(), ^{
    [self.navigationController pushViewController:viewController animated:YES];
   });
});

1 个答案:

答案 0 :(得分:0)

在后台线程上对UI进行任何是一个坏主意。如果你在后台主题并想在主线程上做一些工作,我建议你Grand Central Dispatch (GCD)

dispatch_async(dispatch_get_main_queue(),{
    let vc = myStoryboard.instantiateViewControllerWithIdentifier("your-id-here")
    self.presentViewController(vc, animated: true, completion: nil)
})