ios – Thread 1: Deadly error: No ObservableObject of kind ViewModel discovered


I’m making an attempt to implement a SwiftUI App and having issues with considered one of my observableObjects. Since I’ve two of them and one works completely high quality, I’m struggling to search out out why the opposite one, which is said in the identical approach, is inflicting the error “Thread 1: Deadly error: No ObservableObject of kind EventViewModel discovered. A View.environmentObject(_:) for EventViewModel could also be lacking as an ancestor of this view.”

This is my code:


    func addData(id: String, title: String, footage: [String] ) {
        let db = Firestore.firestore()
        
        db.assortment("occasions").addDocument(knowledge: ["id": id, "name": name,  "pictures": pictures]) {
            error in
            if error == nil {
                print("knowledge efficiently added")
            } else {
                //to do
            }
        }
    }
}

class AppViewModel: ObservableObject{

this is the place I’m declaring them in views:

struct addEventView: View {

    @State var isPickerShowing = false
    @State var selectedImage: UIImage?
    @State var title = ""
    @State var eventID = UUID().uuidString
    @State var footage = [String]()
    
    @EnvironmentObject var viewModel: AppViewModel
    @EnvironmentObject var eventModel: EventViewModel

that is my previews struct:

    static var previews: some View {
        ContentView().setting(.managedObjectContext, PersistenceController.preview.container.viewContext)

    }
}

the error happens when I attempt to add knowledge to Firestone by accessing the strategy through eventModel.addData()… and one way or the other the viewModel works however the eventModel does not

hopefully somebody is ready to assist me

Leave a Reply