Skip to content

Commit d6922b9

Browse files
authored
fix(visionos): multi-scene improvements (#10653)
1 parent e5caa2c commit d6922b9

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

packages/core/platforms/ios/src/NativeScriptMainWindow.swift

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import SwiftUI
22
import NativeScriptEmbedder
33
import 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, *)
614
struct 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

Comments
 (0)