@@ -2,6 +2,14 @@ import SwiftUI
22import NativeScriptEmbedder
33import UIKit
44
5+ // Ensure runtime phases are called only once per app lifecycle
6+ // In multi-scene apps, we need to ensure runtime doesn't reinit alongside main window
7+ // For example, if @State is used to drive @Environment at the root level, we need to ensure
8+ // a rerender of the main body doesn't cause runtime to cycle again
9+ var hasMainInit = false
10+ var hasMainBoot = false
11+ var hasMainSetMainScene = false
12+
513@available ( iOS 14 . 0 , * )
614struct NativeScriptMainWindow : Scene {
715
@@ -18,12 +26,18 @@ struct NativeScriptMainWindow: Scene {
1826 // windowStyle is only supported on visionOS
1927 WindowGroup {
2028 NativeScriptAppView ( found: { windowScene in
21- NativeScriptEmbedder . sharedInstance ( ) . setWindowScene ( windowScene)
29+ if ( !hasMainSetMainScene) {
30+ hasMainSetMainScene = true
31+ NativeScriptEmbedder . sharedInstance ( ) . setWindowScene ( windowScene)
32+ }
2233 } ) . onAppear {
2334 // print("NativeScriptAppView onAppear")
24- DispatchQueue . main. async {
25- NativeScriptEmbedder . boot ( )
26- }
35+ if ( !hasMainBoot) {
36+ hasMainBoot = true
37+ DispatchQueue . main. async {
38+ NativeScriptEmbedder . boot ( )
39+ }
40+ }
2741 } . onReceive ( NotificationCenter . default
2842 . publisher ( for: NSNotification . Name ( " NativeScriptWindowOpen " ) ) , perform: { obj in
2943 let info = parseWindowInfo ( obj: obj)
@@ -68,9 +82,12 @@ struct NativeScriptMainWindow: Scene {
6882 }
6983
7084 init ( ) {
71- NativeScriptViewFactory . initShared ( )
72- NativeScriptEmbedder . sharedInstance ( ) . setDelegate ( NativeScriptViewFactory . shared)
73- NativeScriptEmbedder . setup ( )
85+ if ( !hasMainInit) {
86+ hasMainInit = true
87+ NativeScriptViewFactory . initShared ( )
88+ NativeScriptEmbedder . sharedInstance ( ) . setDelegate ( NativeScriptViewFactory . shared)
89+ NativeScriptEmbedder . setup ( )
90+ }
7491 }
7592
7693 #if os(visionOS)
0 commit comments