App freezes after passing Observable via .environment()

Hello all,

Currently, I'm working on an iOS app that uses an array of Observable classes that are passed through the app using the .environment() function. However, I encountered a problem: the app freezes when "Show SecondLevelDetailView" is tapped in the example below. From the moment the app freezes, I can see that the memory is constantly increasing.

What am I missing?

Thank you in advance for your reply!

The code:

import SwiftUI

@Observable class Model {}
class Model1: Model {}
class Model2: Model {}

// Conformance to Hashable, so that an array of models can be used in a list (see ContentView)
extension Model: Hashable, Identifiable {
    var id: UUID {
        UUID()
    }
    
    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
    
    static func == (lhs: Model, rhs: Model) -> Bool {
        lhs.id == rhs.id
    }
}

struct ContentView: View {
    @State private var models = [Model1(), Model2()]
    
    var body: some View {
        NavigationStack {
            List(models) { model in
                NavigationLink(value: model) {
                    Text("Show DetailView")
                }
            }
            .navigationDestination(for: Model.self) { model in
                DetailView()
                    .environment(model)
            }
        }
    }
}

struct DetailView: View {
    @Environment(Model.self) private var model
    
    var body: some View {
        List {
             // App freezes when this NavigationLink is tapped.
            NavigationLink("Show SecondLevelDetailView") {
                SecondLevelDetailView()
                    .environment(model)
            }
        }
    }
}

struct SecondLevelDetailView: View {
    @Environment(Model.self) private var model
    
    var body: some View {
        Text("Lorem ipsum")
    }
}
Answered by CalciumSulfide in 801531022

Found the solution...

The problem was the Hashable conformance. After removing it and changing ContentView so that it can work without Hashable models, the problem was gone.

Accepted Answer

Found the solution...

The problem was the Hashable conformance. After removing it and changing ContentView so that it can work without Hashable models, the problem was gone.

Great that you were able to fix the problem yourself. It would be great if you could file a bug report since this does not look like correct behavior.

App freezes after passing Observable via .environment()
 
 
Q