A project containing some scenarios on when the SwiftUI
environment gets retained, for
apps integrating UIKit
with SwiftUI
and vice-versa.
- Holding a
View
instance - Holding a reference to a
UIHostingController
instance - Having a
UIView
with a retain cycle - Having a retain cycle between a
UIView
and an environment object
Each scenario has a git tag pointing to it. To explore the scenarios, do the following:
- Begin by checking it out: e.g
git checkout scenario-1
- Run the app in the simulator
- Tap the "Remove RootView from UI" button (when applicable)
- Build the memory graph and look for the environment instances (
EnvA
andEnvB
) - Fix the scenario, properly releasing the environment, making sure it doesn't appear in the memory graph
- When you are done, you can return to git's HEAD:
git switch -
Make sure to check this blog post as well. It'll guide your experience.
Some scenarios might require you to remove the SwiftUI views from the screen. You can do this by tapping at the "Remove RootView from UI" button: