Windows and Views lifecycle visionOS / SwiftUI

I'm not even sure if "lifecycle" is the right term here.

In my project, when certain views are not in my field of view for some time, I've noticed some views undergo state changes, some get sort of "unloaded". This results in multiple errors due to recurring queries expecting loaded variables or instances.

I assumed that utilizing .onDisappear() to unload resources and .onAppear() to reload them as the window/view comes back into my field of view would address these issues. Unfortunately, these methods do not seem to be invoked after being out of sight or inactive for some time.

Is there an effective strategy for managing the states of views under such circumstances? Additionally, is there a specific function that is triggered when the window enters a sort of "stasis" mode due to inactivity or loss of focus?

Accepted Reply

Maybe this can assist you, did you try adding :

@Environment(\.scenePhase) private var scenePhase

And then listening for changes, for example

        .onChange(of: scenePhase) { _, newPhase in
            if newPhase == .background || newPhase == .inactive {
                //do something, or check for `active`
            }
        }
  • This is exactly what I was looking for! tyvm!

Add a Comment

Replies

Maybe this can assist you, did you try adding :

@Environment(\.scenePhase) private var scenePhase

And then listening for changes, for example

        .onChange(of: scenePhase) { _, newPhase in
            if newPhase == .background || newPhase == .inactive {
                //do something, or check for `active`
            }
        }
  • This is exactly what I was looking for! tyvm!

Add a Comment