Saturday, November 21, 2026
HomeiOS Developmentios - Why are objects nonetheless in reminiscence after emptying NavigationStack path?

ios – Why are objects nonetheless in reminiscence after emptying NavigationStack path?

[ad_1]

I am attempting to implement a Coordinator for managing a move. The state is saved contained in the CoordinatorStore. There are 2 @Printed properties for managing the move. The display screen property controls which View is at present proven and path controls the navigation stack of the stack view. Particulars of the implementation could be discovered under.

With the present implementation and after the next actions: showA -> showB -> showInitial

I might count on that StoreA and StoreB can be deallocated from reminiscence since path, which holds StoreA and StoreB by way of enum related values, will get emptied.

However that does not occur, and if I repeat the actions once more there can be 2 StoreA and a couple of StoreB in reminiscence and so forth. Am I lacking one thing?

I will even connect a screenshot of the reminiscence debugger snapshot after doing the preliminary set of actions.

enum Path: Hashable {
    case a(StoreA)
    case b(StoreB)
}

enum Display {
    case preliminary
    case stack
}

closing class CoordinatorStore: ObservableObject {
    @Printed var path: [Path] = []
    @Printed var display screen: Display = .stack
    
    func showA() {
        let retailer = StoreA()
        path.append(.a(retailer))
    }
    
    func showB() {
        let retailer = StoreB()
        path.append(.b(retailer))
    }
    
    func showInitial() {
        path = []
        display screen = .preliminary
    }
    
    func showStack() {
        display screen = .stack
    }
}
struct Coordinator: View {
    @ObservedObject var retailer: CoordinatorStore
    
    var physique: some View {
        change retailer.display screen {
        case .preliminary: preliminary
        case .stack: stack
        }
    }
    
    var stack: some View {
        NavigationStack(path: $retailer.path) {
            VStack {
                Textual content("Root")
            }
            .toolbar {
                Button(motion: self.retailer.showA) {
                    Textual content("Push A")
                }
            }
            .navigationDestination(for: Path.self) { path in
                change path {
                case .a(let retailer):
                    ViewA(retailer: retailer)
                        .toolbar {
                            Button(motion: self.retailer.showB) {
                                Textual content("Push B")
                            }
                        }
                case .b(let retailer):
                    ViewB(retailer: retailer)
                        .toolbar {
                            Button(motion: self.retailer.showInitial) {
                                Textual content("Present Preliminary")
                            }
                        }
                }
            }
        }
    }
    
    var preliminary: some View {
        VStack {
            Textual content("Preliminary")
            Button(motion: retailer.showStack) {
                Textual content("Go to Stack")
            }
        }
    }
}
struct ViewA: View {
    @ObservedObject var retailer: StoreA
    
    var physique: some View {
        Textual content("View A")
    }
}

closing class StoreA: NSObject, ObservableObject {
    deinit {
        print("Deinit: (String(describing: self))")
    }
}
struct ViewB: View {
    @ObservedObject var retailer: StoreB
    
    var physique: some View {
        Textual content("View B")
    }
}

closing class StoreB: NSObject, ObservableObject {
    deinit {
        print("Deinit: (String(describing: self))")
    }
}

Memory Debugger Snapshot deb

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments