How to remove a StateObject from memory

Hi everyone,

I have a content view that declares a StateObject

@StateObject var appModel: AppModel = AppModel.instance

I present the content view from a uikit view:

    let swiftUIView = ContentView()
        let hostingController = UIHostingController(rootView: swiftUIView)
        hostingController.modalPresentationStyle = .fullScreen
        DispatchQueue.main.async{
            self.present(hostingController, animated: true, completion: nil)
        }

I dismiss the contentview this way:

    struct DismissingView: View {
        @Environment(\.dismiss) var dismiss
        
        var body: some View {
            Button(action: {
                dismiss()
            }
                   , label: {
                VStack(spacing: 10) {
                    Image(systemName: "xmark")
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 22)
                }
                .foregroundColor(.white)
            })
        }
    }

when I dismiss the content view I want to deallocate everything, but the stateobject remains. I have tried changing it to an observedObject with no success.

Any idea how to deallocate everything? Thank you

same problem

How to remove a StateObject from memory
 
 
Q