SwiftUI NavigationLink memory leak

As I am watching Data Essentials in SwiftUI I think, I found the answer to my question. It's new StateObject property wrapper (I could not find the documentation, but here is post which describes it). Now, I can use @StateObject, when I want my data to only exists inside a view scope, whithout doing any hacks.


I had the same problem and spent a lot of time to figure it out. Finally, I got it! Use .navigationViewStyle(StackNavigationViewStyle()). Add is as a function to NavigationView:

NavigationView {
   ...
}
.navigationViewStyle(StackNavigationViewStyle())

This is happening because the default navigation style is ColumnNavigationViewStyle, which would show the navigation list AND the selected detail item any time the horizontal size class is regular.

To see this in action run the app on a iPhone Pro Max. Then select an item in portrait, go back, and then rotate the device. Bingo. You'll see the selected item in the second column.

So it turns out that in order to make this magic happen ColumnNavigationViewStyle will remember the selected item until a new item item is selected.

Which in turn gives you the mysterious "retain cycle". It's not a leak, it's just how it works. (Even on devices like the iPhone mini that will never allow the second column to appear.)

The .navigationViewStyle(StackNavigationViewStyle()) fix mentioned elsewhere changes this behavior.