If you do this then there is no need to create a new object model on each push. It is created once and destroyed once on app exit:
@main
struct MyApp: App {
@StateObject var model = Retainer()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(model)
}
}
}
struct RetainCycleView: View {
@EnvironmentObject var model: Retainer
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text("Navigate back to the previous view.")
Text("You will see that 'Retainer' was NOT deallocated.")
Text("(it's deinit function prints deallocing Retainer)")
.font(.callout)
}
.padding()
.searchable(text: $model.enteredText)
.onChange(of: model.enteredText) { newValue in
print("Searching for: ", newValue)
}
}
}