4

I have an issue when I go from viewController A to viewController B, in which it has roughly a 5 second delay before segueing to it. I believe it is due to the amount of views that I'm loading up in viewDidLoad.

I have an xib file that has a stack view of 11 sections that represent levels. Each section has a button and a few images that can change depending on the users progress.

In addition, I instantiate 10 of these xib views to load in a scrollview. This all happens in the viewDidLoad.


I'm wondering if I can load viewController B and have it all ready to go before actually clicking the button that segue's to it; hopefully fixing the delay I get. I'm also using custom segues to and from controllers.

Any help I can get is appreciated. I have looked into this myself, but most topics that I find are outdated, or don't apply. Thanks again for any pointers.

UPDATE: the answer does answer a portion of my question as far as how to prep a view controller for efficiency purposes, it doesn't answer the delay part, but I think I figured it out if you read the comments below answer...

Discoveringmypath
  • 1,049
  • 9
  • 23
  • I do not think the delay is caused by so many views.Or you provide some code here, how you create vc2. – aircraft Feb 12 '17 at 14:32
  • Don't think you can "preload" the view controller, as the life cycle is a low-level feature of segues. But, if you think it's the number of views being loaded (be they the 10 xib files or the rest), you could load them up in VC A and send them to VC B in the prepare for segues function. –  Feb 12 '17 at 14:59

1 Answers1

5

You can move all the heavy code out of viewDidLoad() into some custom method

func prepare() {
 // Something hard 
}

than you can prepare your controller at anytime and store it

var heavyController: HeavyViewController?

override func viewDidLoad() {
  super.viewDidLoad()
  heavyController = HeavyViewController()
  heavyController?.prepare()
}

than just use heavyController in segue instead of creating new one. Hope this helps.

P.S. consider moving heavy parts of code into background thread, you can check the example.

UPDATE: To show your prepared controller using segue do something like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "HeavyController" {
        present(heavyController, animated: true, completion: nil)
    }
}
Ivan Nesterenko
  • 939
  • 1
  • 12
  • 23
  • This seems pretty straight forward. Since I'm using a custom segue though, and I have it setup in Interface builder, Is there a way to change the destination controller to this prepped version? – Discoveringmypath Feb 12 '17 at 18:24
  • So I added the prepare(for segue) and it works as far as the segue goes, but then I get a found nil while unwrapping an optional value for the views. I tried adding loadView() on the prepped version, but that did nothing. I'll have to keep looking at this later but thanks so far, I feel I'm on the right path. – Discoveringmypath Feb 12 '17 at 19:02
  • Add some code, maybe I'll tell you the reason why it happens :) – Ivan Nesterenko Feb 12 '17 at 19:03
  • I added a variable and function and I'm able to run the function from my preppedVC just fine. So the segue works, and the view pops up, but I get the error on a label I try to update. The update occurs in view will appear, to show the current user. If I comment that out, the next error is on my scroll view also when trying to update the scroll view. I don't really understand because the view shows, but the error is when updating, so how could it be nil, if I can see it... I haven't officially moved the heavy lifting yet, I want to get this squared away before doing that... – Discoveringmypath Feb 12 '17 at 19:35
  • So I got the segue to function correctly with storyboard.instantiateViewController, etc... I was even able to run loadView() and then my setup function, but I still get the delay... Maybe it isn't the xib views being configured and added to the scrollview that is causing the delay... – Discoveringmypath Feb 12 '17 at 20:00
  • To check what is taking so long you can use TimeProfiler (xCode -> open dev tools -> Instruments), it will help you to visualize what is taking so long to load. – Ivan Nesterenko Feb 12 '17 at 20:43
  • Just tried out the TimeProfiler, I have no clue what I'm looking at. I don't have a clue on what I'm supposed to be looking for. I will need to look in to this more... Thanks for the tip though. – Discoveringmypath Feb 12 '17 at 23:12
  • I'm able to drill down on when the segue occurs and see certain areas where it takes more time than others. I can post images of this if you think you will be able to tell what is going on from it? I appreciate the help so far. Maybe I'm using too many aspect ratio constraints, so with the amount of constraints it takes some time to lay it all out right... – Discoveringmypath Feb 12 '17 at 23:35
  • 1
    By turning on and off different functionality I am able to get the segue to run faster. I think overall I just have a lot of things going on during that time, and it creates the delay... I think you answered my question about pre-loading for the most part so thanks for that. – Discoveringmypath Feb 13 '17 at 01:20