Videh Jaiswal
Videh Jaiswal

Reputation: 29

need of restoration id in storyboard xcode 9.2 example

What is the need of restoration id in storyboard xcode 9.2 example? I see this IOS Storyboards: Restoration ID just like Storyboard ID? but didn't understand. Can someone help me out with an example.

Upvotes: 0

Views: 1162

Answers (2)

badhanganesh
badhanganesh

Reputation: 3465

TL;DR

A Restoration identifier is set to a UIViewController (In your storyboard usually) when you want to save and restore state of it in your app. View Controllers without a Restoration identifier will not be considered for the saving-and-restoring mechanism.

Why the need for App State Restoration?

Since iOS devices are limited in the amount of RAM that it holds, the system needs to manage that limited resource effectively and share it across the apps that are running. When you run your app, the system (OS) needs to allocate memory for it. If there is enough memory, then fine. If there is not enough memory, the OS determines which suspended app uses most memory and depending on the priority that the OS determines for that app, it will kill/terminate and reclaims that memory and uses it for the highest priority app (Your app that you just opened).


How State Restoration works

Saving State

If you press Home button and put your app to a suspended state, the above process happens for your app too. If some other high-priority app needs memory, if your app consumes much of the resources, then your app will be killed. This is where the state restoration comes into play. If you enable state restoration, whenever you press Home button, the mechanism will store the view controllers that has RestorationId assigned to it, along with its view hierarchy, and the necessary info. that you need to re-construct your app's previous state, to disk.

Restoring State

Assuming that your app is killed by the OS, when you open your app back from the home screen, the state restoration mechanism will retrieve the persisted view controllers and their view hierarchies from the disk and will restore them for you (of course you need to contribute in recreating the app's state), so that the user experience is not affected even if your app was brutally killed by the system.


For more detailed info. follow this SO answer and this awesome tutorial on how to implement it.

Upvotes: 3

MRizwan33
MRizwan33

Reputation: 2741

The Restoration identifier is a string id that you need to assign to any view controller or view that you want preserved and restored. During state preservation any view controllers or views in the view hierarchy that have a restoration identifier will be saved to disk.

The Storyboard Identity in the Identity Inspector, are used to instantiate objects from Storyboard.

There are two things creating object and re-creating object. Restoration preserved your view or any view controller when app quite and can be restore (re-create) your view when you come back.

While Storyboard ID is use to instantiate view controller mean create object. and use to identify the objects on storyboard.

Upvotes: 0

Related Questions