Reputation: 658
What would you think is the best strategy to load an optional UIViewController? Here is the current setup:
I'm using this library https://github.com/flubbermedia/FMRevealViewController which is a custom container view controller that mimics the now very common setup with a mainViewController and a sideViewController which is "revealed" when a user swipes left or right (a la Facebook).
BTW... normally I would set up both the view controllers (main and side) in the AppDidFinishLaunching. However I have an app where the sideViewController takes a little too long to load (around 0.3s). For a number of reasons I can't get around it. At the same time it's essential for my app to load as fast as possible. Finally I can't load it when a user swipes... because it would result in a noticeable delay in the visualization of the content.
What I'd like to achieve then, is some sort of "deferred preloading".
I basically need to preload the sideViewController... but only after the mainViewController is fully loaded and visible on screen.
My first attempt was by using a dispatch_after in the appDidFinishLaunching:
to delay the loading of the sideViewController for two seconds... but it's not a very robust solution.
Now I'm thinking to move the loading of the sideViewController into the viewDidAppear
method of mainViewController... but I'm not crazy about this solution either, I would prefer to keep the two view controller decoupled. And I'm worried on the reliability of viewDidAppear
considering that it's managed by FMRevealViewController and not by the OS.
What would you do?
Upvotes: 0
Views: 294
Reputation: 3854
Try loading the sideViewController on the AppDelegate method applicationDidBecomeActive:
. This delegate will only be called after you app is in active state after launch, so your mainViewController will have loaded already and visible on the Window. Testing on my app, it was only called after viewDidAppear of the Window's rootViewController.
From the docs, “You should use this method to restart any tasks that were paused (or not yet started) while the app was inactive.”
As this delegate method is called every time the app moves from the inactive to active state, you'd just have to check for the existence of the sideViewController so it is initialised only on the app's launch.
Edits based on comments and edits to question
Given that FMRevealViewController is a custom container view controller, mainViewController will be its child contentViewController and sideViewController will be another child VC. So both will be added to the view hierarchy of FMRevealViewController.
In such case, it would make sense to load sideViewController in the viewDidAppear
method of the instance of FMRevealViewController. It wouldn't make sense to initialise it through its sibling mainViewControler (the contentViewController), though, and that seems to be the point of confusion.
So, between making the initialisation of a child VC in viewDidAppear
of the custom container VC or after on applicationDidBecomeActive:
of the delegate, a read from Apples docs would point towards the former: “Your container should make the rules and its children should follow them (...) The [custom container] view controller is responsible for managing its own view hierarchy and other classes should never manipulate its contents.”
Upvotes: 1