Post

Replies

Boosts

Views

Activity

StateObject isn't being deallocated whilst it's containing View has disappeared
Hi! Seems StateObject isn't being deallocated whilst it's containing View has disappeared successfully. Suppose overlay is the cause. StateObject hangs until next view body evaluation happens. import SwiftUI final class Copmanion: ObservableObject { deinit { print("☠️ Copmanion died") } } @MainActor final class SessionManager: ObservableObject { @Published var isActive = false } struct CheckPasscodeView: View { @EnvironmentObject var session: SessionManager @StateObject var copmanion = Copmanion() var body: some View { VStack { Button("Login") { session.isActive = true } } .onAppear { print("CheckPasscodeView appear") } .onDisappear { print("CheckPasscodeView disappear") } } } struct ContentView: View { @StateObject var session = SessionManager() var body: some View { Color.teal .overlay { if !session.isActive { CheckPasscodeView() .environmentObject(session) } else { EmptyView() } } } } ZStack here is one of possible solutions as well as using Observation framework. Conditions: Xcode 16 Xcode 16.1 Beta 2 iphoneosSDK 18.1
1
0
133
Oct ’24