You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's start with the use case that I want to implement. The app supports multiple profiles/personas, which you probably seen in Slack or even Netflix. The user can open multiple windows with different profiles (accounts) or easily switch between them in the same window. This part I have managed to implement, and will share with you below.
But I got stuck on restoration of the scenes. Basically, the @SceneStorqge is not saving data, though it is used in a view. More about it after the basic blocks of the app. I will redact some code to make it simpler and easier to get the point across.
This is pretty much all the main parts. The AppContext holds a list of active sessions and is saved in UserDefaults. So on the next launch I can know which session was with which account Id. And this is mostly works. Though the restoration is broken, or rathe strange to me. Here the flow:
opened the app,
logged in,
have two accounts,
opened another session for the second account
kill the app (state is saved when I opened the second scene)
And now when I open the app, there are three (!) sessions. The first two were restored and there is a new one - empty, without an account id. And this empty one is the primary (visible), the other two are kind of behind the scene, need to tap the tree dots on top of the iPad to switch to them.
Ok, I figured that I am not doing the restoration per se. But rather do it my self in parallel, so issues like that are expected. My next step would be to integrate into the restoration flow, which is better to be done with the @SceneStorage. And that's where I get into things that I don't understand. I followed the WWDC 23 video about @SceneStorage and added this code to the view (also is above):
struct ATAppView:View{@SceneStorage("accountId")varsceneAccountId:String?.....task{
// monitor changes to ViewStore, specifically for SceneStorage
forawait_in viewStore.objectWillChange.values {
sceneAccountId = viewStore.accountId
}}
The most frustrating thing is that the above code doesn't save the accountId. I can put the breakpoint at the line where the sceneAccountId is being updated and after this line is executed the value still nil.
And in the scene delegate (passed to the ATScene) I get the restoration activity, which somehow has persistentIdentifier that never was in any of the scenes before, and that's how it becomes an empty scene
if let userActivity = connectionOptions.userActivities.first ?? session.stateRestorationActivity {
I always get empty accountId, it is there as a record in the dictionary, but empty. I've tried to close the app and see if the changes to the scene phase can help, nope, still nothing is saved. I tried to put that code in .onChange, still doesn't work. And I cannot figure out why. No errors, like "SceneStorage can only be used on view" or such. It's just silently does nothing.
Any ideas, or comments, would be appreciated. And if you have any questions about the code or missing pieces, let me know.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Let's start with the use case that I want to implement. The app supports multiple profiles/personas, which you probably seen in Slack or even Netflix. The user can open multiple windows with different profiles (accounts) or easily switch between them in the same window. This part I have managed to implement, and will share with you below.
But I got stuck on restoration of the scenes. Basically, the
@SceneStorqge
is not saving data, though it is used in a view. More about it after the basic blocks of the app. I will redact some code to make it simpler and easier to get the point across.Info.plist
AppDelegate:
SceneDelegate:
Scene - scenes that app supports with ability to open a new scene
AppContext and AppScene - serializable state of the sessions (like account ID, theme, etc)
Main view:
Main Feature: Reducer and View
This is pretty much all the main parts. The AppContext holds a list of active sessions and is saved in UserDefaults. So on the next launch I can know which session was with which account Id. And this is mostly works. Though the restoration is broken, or rathe strange to me. Here the flow:
And now when I open the app, there are three (!) sessions. The first two were restored and there is a new one - empty, without an account id. And this empty one is the primary (visible), the other two are kind of behind the scene, need to tap the tree dots on top of the iPad to switch to them.
Ok, I figured that I am not doing the restoration per se. But rather do it my self in parallel, so issues like that are expected. My next step would be to integrate into the restoration flow, which is better to be done with the
@SceneStorage
. And that's where I get into things that I don't understand. I followed the WWDC 23 video about@SceneStorage
and added this code to the view (also is above):The most frustrating thing is that the above code doesn't save the
accountId
. I can put the breakpoint at the line where thesceneAccountId
is being updated and after this line is executed the value stillnil
.And in the scene delegate (passed to the
ATScene
) I get the restoration activity, which somehow has persistentIdentifier that never was in any of the scenes before, and that's how it becomes an empty sceneI always get empty
accountId
, it is there as a record in the dictionary, but empty. I've tried to close the app and see if the changes to the scene phase can help, nope, still nothing is saved. I tried to put that code in.onChange
, still doesn't work. And I cannot figure out why. No errors, like "SceneStorage can only be used on view" or such. It's just silently does nothing.Any ideas, or comments, would be appreciated. And if you have any questions about the code or missing pieces, let me know.
Beta Was this translation helpful? Give feedback.
All reactions