What is the difference between -viewWillAppear: and -viewDidAppear:?
In general, this is what I do:
ViewDidLoad - Whenever I'm adding controls to a view that should appear together with the view, right away, I put it in the
ViewDidLoad
method. Basically, this method is called whenever the view was loaded into memory. So for example, if my view is a form with 3 labels, I would add the labels here; the view will never exist without these forms.ViewWillAppear: I use
ViewWillAppear
usually just to update the data on the form. So, for the example above, I would use this to actually load the data from my domain into the form. Creation ofUIViews
is fairly expensive, and you should avoid as much as possible doing that on theViewWillAppear
method because when this gets called, it means that the iPhone is already ready to show theUIView
to the user, and anything heavy you do here will impact performance in a very visible manner (like animations being delayed, etc).ViewDidAppear: Finally, I'm using
ViewDidAppear
to start off new threads to things that would take a long time to execute, like for example doing a web service call to get extra data for the form above. The good thing is that because the view already exists and is being displayed to the user, you can show a nice "Waiting" message to the user while you get the data.
viewDidLoad ===>>> Put your initialization code here. Don't put dynamic data that might change during the view lifecycle. So, if you are pulling data from core data you don't want to do it here if this could change during the life of the view. For example: say you have a tab controller. You switch from tab1 to tab2 and change something on the model in tab2. If you come back to tab1 and your model code was done in viewDidLoad this would not be updated (assuming you're not using KVO or NSFetchedResultsController, etc.).
viewWillAppear ===>>> This is called every time the view is about to appear, whether or not the view is already in memory. Put your dynamic code here, such as model logic.
viewDidAppear ===>>> Put expensive operations here that you only want to do if you're sure the view is onscreen, such as network calls.
Notice: if your app is backgrounded and returns to the foreground you need to handle this using NSNotificationCenter. I wrote the code out for that in the comments below. You might think viewWillAppear/viewDidAppear will fire. Put a break point there and test it. It doesn't fire. So, if something has changed for your app while it was in the background you'll need to update that using notifications.
The viewWillAppear
method is called before loading the actual view.
The viewDidAppear
method is called when the view is already loaded, and you want to show something.
viewWillAppear:
■ Called before the view is added to the windows’ view hierarchy
■ Called before [vc.view layoutSubviews] (if necessary)
viewDidAppear:
■ Called after the view is added to the view hierarchy
■ Called after [vc.view layoutSubviews] (if necessary)