A project containing some scenarios on when the SwiftUI environment gets retained, for
apps integrating UIKit with SwiftUI and vice-versa.
- Holding a
Viewinstance - Holding a reference to a
UIHostingControllerinstance - Having a
UIViewwith a retain cycle - Having a retain cycle between a
UIViewand 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 (
EnvAandEnvB) - 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:
