iOS: Support starting EventLoop from already running UIApplication #4413
+84
−24
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.
These are the minimal code changes I could get away with to enable starting a
EventLoopwith an already-runningUIApplication. The use case would be runningwinit-based apps inside any sort of already-running app on iOS, either from a native SwiftUI/Objective-C application or via Python/PyO3. Basically any case were you can't/don't want to control whenUIApplicationMainis called.Please treat this as a starting point for discussion! Feedback is very welcome.
There are some major limitations at the moment:
run_appblocks indefinitely by parking the calling thread. This is obviously not great for several reasons, and it also means you can't create theEventLoopon the main thread.MainThreadMarker. In my testing this did not cause problems (as the callbacks that contain the code that calls into UIKit still run on the main thread), but it is massively unsafe and likely invites potential unsoundness.EventLoop. I'm actually not sure what the best approach is here. One could either:(a) Adopt a web-style
register_appthat returns immediately and then provide a way to signal that the app has finished running, or (b) provide arun_appthat blocks for the duration of the app and then returns. The latter would obviously require calling it from a new thread.winitinstances simultaneously because they name custom Objective-C classes the same way. Not problem when you can't run more than oneEventLoopperUIApplication, but problematic once you lift that restriction. This could potentially be addressed by generating unique names at runtime (?).Please let me know what you think!
changelogmodule if knowledge of this change could be valuable to users