Skip to content
tezvyn:

UIViewController lifecycle states: network vs geometry updates

Source: developer.apple.comEasyHow cards are made

UIViewController lifecycle states: network vs geometry updates

Tests UIViewController lifecycle states and separation of data vs layout. Strong answers list loadView to viewDidDisappear, start network in viewDidLoad or viewWillAppear, and geometry in viewDidLayoutSubviews. Red flag: network calls inside layout methods.

What's really being asked

This question checks whether you understand the UIViewController state machine and can distinguish between view creation, visibility, and layout events. Interviewers want to see that you know which hooks are safe for expensive one-time work, which recur every time the view appears, and which fire when geometry changes.

The full answer

First, loadView, which only runs if the view property is nil and lets you build a view hierarchy programmatically. Second, viewDidLoad, called once after the view is created or loaded from a storyboard, ideal for one-time setup and initial network requests. Third, viewWillAppear and viewDidAppear, which run every time the view is added to the window hierarchy; viewWillAppear can trigger a refresh if data might be stale. Fourth, viewWillDisappear and viewDidDisappear, used to stop timers, save state, or end editing. Fifth, viewWillLayoutSubviews and viewDidLayoutSubviews, which fire when the view bounds change; viewDidLayoutSubviews is the correct place to read final bounds and update frames or layout-dependent layers after Auto Layout has finished. A strong candidate explicitly separates network work from layout work, placing requests in viewDidLoad or a coordinator rather than in layout methods.

The mistakes people make

A frequent mistake is initiating network requests inside viewDidLayoutSubviews, causing repeated fetches on rotation or split-view resize. Another is computing frames or reading bounds in viewDidLoad before the view has resolved its final size or safe area insets. Some candidates also claim all setup belongs in viewWillAppear, which wastes resources because that method runs every time a navigation controller pushes or pops back to the screen.

What usually comes next

The interviewer may ask where to register and unregister observers or notifications, which is typically viewWillAppear and viewDidDisappear. They may ask how traitCollectionDidChange or viewWillTransition fit into the lifecycle. They may also probe whether you understand the difference between viewDidLoad and init, or how to handle memory warnings via didReceiveMemoryWarning.

A concrete example

Consider a social feed profile screen. In viewDidLoad you create the table view, configure constraints, and start a network request to fetch the user model. In viewDidLayoutSubviews you read the final view bounds and update a custom avatar background gradient layer so its frame matches the header width exactly. You do not set that layer frame in viewDidLoad because safe area insets and final bounds are not guaranteed there, especially on devices with dynamic islands or when the view is embedded in a container.

Interview question

Which approach correctly places a one-time network request and a bounds-dependent layout update in a UIViewController?

  • a.Start the request in viewDidLoad and update frames in viewDidLoad
  • b.Start the request in viewWillAppear and update frames in viewDidLayoutSubviews
  • c.Start the request in viewDidLayoutSubviews and update frames in viewDidLayoutSubviews
  • d.Start the request in viewDidLoad and update frames in viewDidLayoutSubviewsCorrect
Why?

viewDidLoad runs once after the view is created, making it the right place for an initial network request, while viewDidLayoutSubviews fires after Auto Layout resolves final bounds so frames are accurate there. Updating frames in viewDidLoad is a common mistake because safe area insets and final bounds are not yet guaranteed at that point.

Just read this? Test yourself on what you have been reading.

Read the original → developer.apple.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on ios — each one lists the topics its interview covers.

See open roles