Skip to content

Instantly share code, notes, and snippets.

@iOSappssolutions
iOSappssolutions / gist:8607d1b316dab94da1d219e5efcf7d70
Created November 4, 2023 23:14
Tapping on Menu activates NavigationLink in visionOS
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationStack {
VStack {
NavigationLink(value: "1") {
HStack {
Text("Test menu")
@iOSappssolutions
iOSappssolutions / FB11835461.md
Last active May 12, 2023 13:44
NavigationStack creates memory leak if Hashable referrence type is used for path

FB11835461

In real world applications it makes more sense to use ObservableObject referrence type to drive navigation than simple value type. The example below demonstrates that.

Problem is that for some reason NavigationStack will create memory leak if path is set to empty array after navigating any number of times through stack. All view models that were created in process will be retained in memory even though path is set to [].

Same problem occurs if we set path to empty array and just swap whole NavigationStack with any other view still all objects created will be retained in memory.

There is workaround that I will show bellow to fix this but it is still very buggy behaviour IMO.