Bind the Android main looper via swift-android-native#69
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Imports swift-android-native and binds the Android main looper to its
AndroidMainActorat app launch, so@MainActor/DispatchQueue.main/ Swift concurrency dispatch correctly on Android without hand-drainingRunLoop.main.What changed
AndroidSwiftUIpackage, with theAndroidLooperproduct on theAndroidSwiftUItarget (Android-only). Same package identity as thePureSwift/Androiddependency, so it's pinned to the same fork/branch (MillerTechnologyPeru @ feature/pureswift) to keep the graph on a single revision. TheCoreFoundationtrait is enabled — that's the setting that drains the dispatch main queue throughCFRunLoopRunInMode.Application.onCreateSwift()(runs on the main thread at process start) now callsAndroidMainActor.setupMainLooper().MainActivity.runAsync()— the fragileRunLoop.main.run(until:)warmup, now redundant with the looper bound.Reviewer note — why the render scheduler is still
Handler.postI attempted to simplify the interpreter's re-render scheduler from the manual
Handler.posttoDispatchQueue.main.asyncnow that the looper is bound. It crashes, and the trait does not fix it:Rendering makes JNI calls (materializing the
ViewNodetree), and JNIFindClassresolves against the class loader of the Java frame on the stack.Handler.postruns the render inside aRunnable.run()frame carrying the app's class loader; the dispatch-queue drain runs in a native context whose fallback boot class loader can't see the app's classes. TheCoreFoundationtrait changes how the queue drains, not the class-loader context — so it can't help here. This is a swift-java limitation (would needFindClassto use a cached appClassLoaderon native threads). The scheduler therefore staysHandler.post, with the reason documented inline.AndroidMainActor/DispatchQueue.mainremain correct for non-JNI main-thread work.Verification
AndroidMainActor.setupMainLooper() -> truein logcat), navigation works, and state re-renders (tapped a button, counter reachedTaps: 3) — no crash.SwiftUICoretests pass.