How to transition between scenes when using SpriteView in SwiftUI

I have two simple scenes. One is a Game Scene and the other is a Game Over Scene:

class TestGameScene: SKScene {
    override init(size: CGSize) {
        super.init(size: size)
        print("Initialising Test Game Scene \(ObjectIdentifier(self))")
        let textNode = SKLabelNode(text: "Game Scene")
        textNode.position.x = frame.midX
        textNode.position.y = frame.midY
        addChild(textNode)
    }
    
    // required init omitted for clarity ...
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view!.presentScene(TestGameOverScene(size: size))
    }
}
class TestGameOverScene: SKScene {
    override init(size: CGSize) {
        super.init(size: size)
        print("Initialising Game Over Scene \(ObjectIdentifier(self))")
        let textNode = SKLabelNode(text: "Game Over")
        textNode.position.x = frame.midX
        textNode.position.y = frame.midY
        addChild(textNode)
    }
    
    // required init omitted for clarity ...

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view!.presentScene(TestGameScene(size: size))
    }
}

and I display the scene using a simple SpriteView

struct ContentView: View { 
    var body: some View { 
        SpriteView(scene: TestGameScene(size: CGSize(width: 300, height: 300)))
    }
}

From within the TestGameScene I want to present the TestGameOverScene when the user touches the screen. This works as expected except when I make the app go into the background. When the app goes into the background as the TestGameOverScene is visible, the TestGameOverScene deinitialises for some reason. This causes the original TestGameScene to move to the view again.

I initially thought SwiftUI was recomputing the body property or something but it turns out that it wasn't. The issue has got to do with something else that I can't figure out.

What's the correct way to transition between scenes without unnecessarily keeping both scenes in memory? I want one to deinitialise when the other is transitioned to. Or does SpriteView simply not support this sort of thing? Any advice would be appreciated.

After messing around with a few ideas I came up with solution that works, however, I would love some criticism / analysis of how good this method is as I don't know much about memory management.

This class provides ContentView with the scene to render using SpriteView. It's a published property as when it changes, SwiftUI should be notified.

final class SceneProvider: ObservableObject { 
    @Published var scene: SKScene

    init() { 
        let initialScene = TestGameScene()
        self.scene = initialScene
        initialScene.viewState = self
    }
}

Added a binding/reference to the scene being presented by SwiftUI. We'll update this reference to the newly presented scene to notify SwiftUI.

class TestGameScene: SKScene { 
    unowned var viewState: SceneProvider!
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let sceneToPresent = TestGameOverScene()
        viewState.scene = sceneToPresent  // << we do this so SwiftUI updates
        view!.presentScene(sceneToPresent, transition: .fade(withDuration: 1.0))
    }
    // .. Rest is the same
}

From here, I could perhaps create a custom SKScene subclass that encapsulates all of this behaviour.

And of course,

struct ContentView: View { 
    @StateObject var sceneProvider = SceneProvider()  // << @StateObject ensures the container for the presented scene, ie. SceneProvider, isn't deallocated
    
    var body: some View { 
        SpriteView(scene: sceneProvider.scene)
    }
}

Using this method, TestGameScene gets deinitialised when the TestGameOverScene is presented - which is good (I think). In addition to this, SwiftUI maintains the newly presented scene even if you go to the background and come back as the @Published property along with @StateObject keeps SwiftUI up to date.

As there is only one scene held by SceneProvider, we don't have multiple scenes just taking up space in memory - which is good again (this I know is good for sure).

Would love some feedback on this.