Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can happen between viewWillAppear and viewDidAppear?

I am optimizing a transition that seems to be slow on my device. I am pushing one UIViewController from another when a UITableView's row is selected. There is a noticeable pause after row selection and before the new view is pushed.

Some logging indicates that all of my code is reasonably quick, from row selection until the pushed controller's viewWillAppear. But then the time between viewWillAppear and viewDidAppear is logged at around 0.7 seconds.

The transition itself (I believe) should only take 0.3 seconds. What could be accounting for the remainder?

I am testing on an iPhone 4, so I'm not expecting the snappiest performance. But I should be able to match the same performance of other similar apps on the same device, no?

like image 793
Ben Packard Avatar asked Oct 20 '12 18:10

Ben Packard


People also ask

Does viewWillAppear get called before viewDidLoad?

viewWillAppear(_:)Always called after viewDidLoad (for obvious reasons, if you think about it), and just before the view appears on the screen to the user, viewWillAppear is called.

What is the difference between viewDidLoad and viewDidAppear?

The difference between viewDidAppear and viewDidLoad is that viewDidAppear is called every time you land on the screen while viewDidLoad is only called once which is when the app loads.

What is called after viewDidAppear?

viewDidAppear is called once you see the loaded view on screen. It is called after view appeared.

How many times is viewWillAppear called?

If both these methods are called, what's the difference between the two? viewDidLoad() is only called once, when the view is loaded from a . storyboard file. viewWillAppear(_:) is called every time the view appears.


2 Answers

I had a similar question a few weeks ago, and I wrote a blog post about what I found:

http://bradbambara.wordpress.com/2014/07/31/object-life-cycle-uiviewcontroller/

The TL;DR version is that iOS will:

  • perform the layout of your new scene
  • perform the transition to your new scene (if it's an animated transition)

...so my guess is the delay could be caused by an especially long transition, or if you're doing any performance-intensive work in your layout code.

like image 111
BradB Avatar answered Nov 10 '22 12:11

BradB


The transition itself (I believe) should only take 0.3 seconds. What could be accounting for the remainder?

Resources are usually consumed in the following methods: drawRect:, layoutSubviews, viewDidLoad, viewWillAppear:. Also, loading from NIB may require quite much time.

After viewWillAppear:, iOS will make a snapshot of the new (and probably current) view to perform smooth animation between two screens. So make sure that drawing and layout code for both controller views is fast enough.

like image 25
Vadim Avatar answered Nov 10 '22 14:11

Vadim