简体   繁体   中英

How to load storyboards in the background?

I have split my app into multiple storyboards, called upon programatically. How should I load them in the background, so the user won't have to wait for seconds before the app would respond, especially on slower devices like iPad 2 and iPhone 4s?

Unfortunately, it is not documented (at least I cannot remember where) if creating UIStoryboard from a background thread is allowed or not. Typcially, UIKit classes are not thread safe and should only be used from the main thread. So you need to investigate yourself.

Technically, UIStoryboard.init(name:bundle:) should not cause that much delay, because when XCode compiles your project, it will split up your storyboard into single NIB files (each for one view controller, as a serialzed object graph) and just reference them. So most of the runtime cost should be instantiating the view controllers from that storyboard, not the storyboard itself.

But you could try yourself and check the runtime cost:

  • Create a "global" dictionary
  • instantiate all your storyboards using some GDC background thread
  • and store them (using the storyboard's name as the key) in that dictionary
  • maybe also intantiate all the view controllers (and store them as sub-entries)

But you'll need to synchronize the access to that dictionary, because the main thread might want to access it simultaneously. Even worse, maybe your main thread has to wait until your background thread has created all the storyboards in your app, which will take even longer than just simply creating them on demand, so the startup time might be significantly longer.

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