Delay when using instantiateViewControllerWithIden

2020-02-02 03:02发布

The code below is used to push another view controller onto the navigation stack.

When using instantiateViewControllerWithIdentifier, the segue is noticeably sluggish the first time (~3 seconds) but occurs reasonably fast each subsequent time. Other SO posts suggested ensuring the segue occurs on the main thread, which the code accomplishes, but this didn't fix the problem.

However, using performSegueWithIdentifier causes no delay.

The viewDidLoad code for SendViewController is the same for the first and subsequent pushes.

Tried blanking out viewDidLoad for the destination view controller, but still the lag exists for instantiateViewControllerWithIdentifier but not for performSegueWithIdentifier.

How to fix the delay with instantiateViewControllerWithIdentifier?

No delay:

@IBAction func buttonTapped(sender: UIButton) {
    performSegueWithIdentifier(SendSegue, sender: self)  
}

Results in delay when showing SendViewController for first time:

@IBAction func buttonTapped(sender: UIButton) {
    dispatch_async(dispatch_get_main_queue()) {
        let vc = self.storyboard!.instantiateViewControllerWithIdentifier(self.SendViewControllerID) as! SendViewController
        self.navigationController!.pushViewController(vc, animated: true)
    }   
}

3条回答
干净又极端
2楼-- · 2020-02-02 03:39

This issue could occur in many different scenarios. The best way determine what is causing your specific problem is by profiling with the instruments included in Xcode.

  1. Click and hold the Build button in your xcode window. You will see four options appear, select Profile.
  2. Once the build runs a window with instruments will pop up. Select, Time Profiling from the options.
  3. A new window will appear with various metrics in it. The top left corner will have a red record button. Click the red record button and this will launch the app on your phone.
  4. Proceed to the transition giving you problems. End the recording after the transition occurs by selecting the same button you started the recording with.
  5. Review the "Details" pane in the bottom left corner. You will see a column titled "Running time" that shows the time it took to execute every method in your code (both OS methods and user generated code)
  6. Determine if anything is out of place or occurs that is not intended. Possibly go back and execute the transition again to compare the difference between the two. Clicking the function in the list will take you directly to the code being executed. This can be very helpful.

It is very likely that if a transition takes 3-5 seconds one particular function will be obvious when following these steps. Happy profiling!

WWDC from last year has a great segment on this as well. Def worth checking out here: (open in Safari only) WWDC Profiling Talk

查看更多
手持菜刀,她持情操
3楼-- · 2020-02-02 03:47

The problem was isolated to the presence of a UITextField in the destination view controller, that is, removing the UITextField removes the lag.

Then it was further isolated to the presence of a custom font.

In other words, using the system font on the UITextField, rather than a custom font, removes the lag. No explanation why, but it works.

查看更多
对你真心纯属浪费
4楼-- · 2020-02-02 03:47

After time profiling I realized it was the call to instantiateViewController which I couldn't find anything that could help me with that.

Unfortunately, the only thing that worked was either using a separate storyboard for that view controller and instantiating it from there, or redoing the view controller programmatically.

查看更多
登录 后发表回答