简体   繁体   中英

UIStoryboard instantiateViewControllerWithIdentifier: Can we call this in background thread?

I'm trying to call instantiateViewControllerWithIdentifier of storyboard in background thread. But I'm presenting it on main thread.

Is there good way to do so? Please give me your suggestions.

My code is something like this.

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];
   });
});

Its a bad idea to do anything to the UI on a background thread. If you're on a background thread and want to do some work on the main thread, I'd recommend Grand Central Dispatch (GCD) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM