@@ -2,6 +2,14 @@ import SwiftUI
2
2
import NativeScriptEmbedder
3
3
import UIKit
4
4
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
+
5
13
@available ( iOS 14 . 0 , * )
6
14
struct NativeScriptMainWindow : Scene {
7
15
@@ -18,12 +26,18 @@ struct NativeScriptMainWindow: Scene {
18
26
// windowStyle is only supported on visionOS
19
27
WindowGroup {
20
28
NativeScriptAppView ( found: { windowScene in
21
- NativeScriptEmbedder . sharedInstance ( ) . setWindowScene ( windowScene)
29
+ if ( !hasMainSetMainScene) {
30
+ hasMainSetMainScene = true
31
+ NativeScriptEmbedder . sharedInstance ( ) . setWindowScene ( windowScene)
32
+ }
22
33
} ) . onAppear {
23
34
// 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
+ }
27
41
} . onReceive ( NotificationCenter . default
28
42
. publisher ( for: NSNotification . Name ( " NativeScriptWindowOpen " ) ) , perform: { obj in
29
43
let info = parseWindowInfo ( obj: obj)
@@ -68,9 +82,12 @@ struct NativeScriptMainWindow: Scene {
68
82
}
69
83
70
84
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
+ }
74
91
}
75
92
76
93
#if os(visionOS)
0 commit comments