Skip to content

Commit 744e443

Browse files
committed
Marshal re-renders onto the Android main looper
1 parent a8a0c0c commit 744e443

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

Sources/AndroidSwiftUI/SwiftUIHostView.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77

88
import AndroidKit
9+
import JavaLang
910
import AndroidSwiftUIBridge
1011
import AndroidSwiftUICore
1112

@@ -36,7 +37,13 @@ public enum AndroidSwiftUIApp {
3637
assertionFailure("host view has no tree store")
3738
return
3839
}
39-
let runtime = BridgeRuntime(root: root, store: store)
40+
// marshal re-renders onto the main looper: JNI object creation and
41+
// Compose state writes must run there, not on a Swift Task's thread
42+
let handler = AndroidOS.Handler(try! JavaClass<AndroidOS.Looper>().getMainLooper())
43+
let runtime = BridgeRuntime(root: root, store: store) { block in
44+
let runnable = Runnable { block() }
45+
_ = handler.post(runnable.as(JavaLang.Runnable.self))
46+
}
4047
Self.runtime = runtime
4148
runtime.start()
4249
activity.setRootView(host)

0 commit comments

Comments
 (0)