Skip to content
tezvyn:

Outline the key steps and APIs for State Preservation and Restoration

Source: developer.apple.comHardHow cards are made

Outline the key steps and APIs for State Preservation and Restoration

Tests UIKit's automatic state restoration pipeline. Strong answers cover: opting in via AppDelegate, setting restorationIdentifiers, implementing encode/decodeRestorableStateWithCoder, and the system snapshot cycle.

What's really being asked

This tests whether you know UIKit's automatic state restoration machinery end-to-end, not just manual persistence patterns. Senior engineers should distinguish between system-driven preservation and ad-hoc archiving, and they should know the exact delegate methods and protocol methods involved.

The full answer

A good answer hits four things in order. First, the opt-in step: the app delegate must implement application:shouldSaveApplicationState and return YES, plus application:shouldRestoreApplicationState on launch. Without these, UIKit skips the entire pipeline. Second, the identifier chain: every view controller and container view controller in the target hierarchy must have a non-nil restorationIdentifier. UIKit uses these identifiers to encode the graph structure into a keyed archive, so if a navigation controller lacks one, the entire branch below it is lost. Third, the state encoding: each view controller should override encodeRestorableStateWithCoder to write transient properties like text field contents, scroll offsets, or selected segments, and override decodeRestorableStateWithCoder to read them back. Fourth, the lifecycle hook: the system calls application:willEncodeRestorableStateWithCoder at the start of preservation and application:didDecodeRestorableStateWithCoder after restoration, giving the app a chance to store global metadata. Strong candidates also mention viewControllerWithRestorationIdentifierPath:coder on the AppDelegate or view controller classes to handle custom instantiation when storyboards are not used.

The mistakes people make

Common wrong answers include describing a manual snapshot of the view hierarchy, using UserDefaults to stash view controller class names, or relying on applicationDidEnterBackground to do the work. Another red flag is forgetting that restorationIdentifiers must be set on every ancestor container, not just the leaf view controllers. Some candidates also confuse this with Scene State Restoration via NSUserActivity, which is a separate but related API.

What usually comes next

Interviewers often follow up by asking how you handle custom view controllers that cannot be instantiated from a storyboard, which is exactly what viewControllerWithRestorationIdentifierPath is for. They may also ask how you preserve the state of a custom view embedded in a view controller, which requires implementing UIStateRestoring on that view and attaching it via restorationIdentifier. Another common follow-up is how state restoration interacts with modern scene-based apps and multiple windows.

A concrete example

Imagine a tab bar controller with two tabs, each a navigation stack. To preserve this, you set restorationIdentifier on the UITabBarController, each UINavigationController, and every UIViewController in the stacks. In a detail view controller, you implement encodeRestorableStateWithCoder to write coder.encode(self.searchText, forKey: "searchText"), then in decodeRestorableStateWithCoder you read it back and apply it to the search bar. If the detail view controller is not in a storyboard, you implement viewControllerWithRestorationIdentifierPath in the app delegate, inspect the identifier path array, and return a freshly allocated instance configured with the provided coder.

Interview question

Why must every container view controller have a non-nil restorationIdentifier during UIKit state restoration?

  • a.They map view controller class names to NSUserActivity entries for scene-based restoration
  • b.They allow UIKit to encrypt the keyed archive that stores the view controller graph
  • c.They signal the system to capture a visual snapshot of that branch for faster relaunch
  • d.UIKit uses them to reconstruct the graph structure, so a missing identifier loses the entire branchCorrect
Why?

UIKit uses restorationIdentifiers to encode the view controller hierarchy into a keyed archive, so a missing identifier on any container severs that branch entirely. Option C reflects the common misconception that restoration relies on manual visual snapshots rather than encoded graph archives.

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