The StateObject doesn't deinit, if I use it's @Published var in an .alert() in iOS15. Here is the minimal code to reproduce the problem:
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
MainUIView()
}
}
}
struct MainUIView: View {
var body: some View {
NavigationView {
NavigationLink("Go to Child View", destination: ChildView())
}
.navigationViewStyle(.stack)
}
}
struct ChildView: View {
@StateObject var vm = ChildViewModel()
var body: some View {
Text("Hello, World!")
//HERE. I have to use $vm.showingAlert from the StateObject:
.alert("Alert Title", isPresented: $vm.showingAlert, actions: {
Button("OK") { }
}, message: {
Text("Alert Message")
})
}
}
@MainActor final class ChildViewModel: ObservableObject {
@Published var showingAlert = false
init() {
print("INIT")
}
deinit {
print("DE-INIT")
}
}
If you go to ChildView and back - you won't see "DE-INIT" printed in the console on iOS15. Is it a bug or is my code wrong?
PS Yes I need the showingAlert var to be in the ChildViewModel as it has business logic which shows an Alert in the View.