App State Restoration odd behavior

I'm working on an app that presents a series of view controllers. There is no navigation controller because once the user goes through a view, we want him to continue to the next view. I've implemented the basic boiler plate delegation methods for restoration, namely application(_:shouldSaveApplicationState:) and application(_:shouldRestoreApplicationState:). I also made sure that all my view controllers have their restoration ID set. I'm doing all of this because if the user goes in the privacy settings and changes one of the settings for our app, the app is terminated. The next time the user opens the app, I obviously want him to land exactly where he left. It's kinda working except with one twist: all the view controllers that were presented before the last one are briefly shown. That's not exaclty what I would want. I would want to only display the last view controller that was shown before the user went to the Settings.


I tried implementing application(_:viewControllerWithRestorationIdentifierPath:coder:). This method gets called multiple times when the app relauinch. Initially, the array of restoration IDs has only the restoration ID of the first view controller that was displayed. Then, the method is called again and this time both the restoration IDs of the first 2 view controllers are in the array. And it does this 4 times, each time, the array has an additional restorarion ID for a view controller that was displayed. I followed the directions and tried to return the view controller matching the last restoration ID in the array but that didn't change anything as each view controller is displayed.


Obviously, I'm missing something. Anybody has any idea?


Thanks in advance!

Replies

It sounds like you need to look at what's happening in your applicationWillFinishLaunching and applicationDidFinishLaunching


applicationWillFinishLaunching takes place before state restoration

applicationDidFinishLaunching takes place after


the pattern i uses is to check in applicationDidFinishLaunching if state restoration occurred (inside shouldRestoreApplicationState)

and if so you canskip initializing the objects that you would normally initialize on a cold launch as they are already restored (ie not nil)