Unpredictable display of composite Scenes in MacOS App

Folks,

I’m writing an app which asks its user for credentials, logs into a server, retrieves data using the credentials entered, and then presents the data on screen. Very simple (so far).

I thus have written a SwiftUI app that chains two scenes, corresponding to the two phases of the app:

import SwiftUI

@main
struct MyApp: App {
    @StateObject var stateObjectInstance = StateObject ()

    var body: some Scene {
        Window {
            if !stateObjectInstance.hasValidCredentials {
                someViewToAskAndCheckForCredentials
            }
        }

        Window {
            if stateObjectInstance.hasValidCredentials {
                someViewToDisplayData
            }
        }
    }
}

Problem is, I get inconsistent behaviour. This work the first time I execute the code. Then, it doesn’t: either both, or only the second, scene(s) don’t show up. If I swap the Window {} blocks in the code, then it works fine again, but only once.

So my question is: is this expected behaviour because I got something wrong, is this caused by, say, some persistent state being restored whereas it should not, or is this a bug?

Thanks a bunch!

Needless to say, this work fine if the logic is written in a single Window scene, so there’s definitely something amiss with the scene display.

Unpredictable display of composite Scenes in MacOS App
 
 
Q