Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

ios - Delay when using instantiateViewControllerWithIdentifier but not performSegueWithIdentifier?

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)
    }   
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...