CoreData: Operation could not be completed 'Foundation_GenericObjError error 0'

I'm encountering the error 'Operation could not be completed 'Foundation_GenericObjError error 0'' when loading my canvas on my SignupView.

No errors are showing in my code, I've tried all sorts of fixes and nothing seems to work.

Any help would be appreciated, if you need more code I will update post.

SignupView

@Environment(\.managedObjectContext) private var viewContext

@FetchRequest( sortDescriptors: [NSSortDescriptor(keyPath: \Account.userSince, ascending: true)], animation: .default)

      private var savedAccounts: FetchedResults<Account>

.onAppear(perform: userData)
.fullScreenCover(isPresented:$showHomeView, content: {

            HomeView().environment(\.managedObjectContext, viewContext)

func userData() {

        Auth.auth().addStateDidChangeListener { auth, user in

            if let currentUser = user {

                if savedAccounts.count == 0 {

                    // Add data to Core Data

                    let userDataToSave = Account(context: viewContext)

                    userDataToSave.name = currentUser.displayName

                    userDataToSave.userID = currentUser.uid

                    userDataToSave.numberOfCertificates = 0

                    userDataToSave.profileImage = nil

                    userDataToSave.userSince = Date()

                    do {

                        try viewContext.save()

                        DispatchQueue.main.async {

                            showHomeView.toggle()

                        }

                    } catch let error {

                        alertTitle = "Could not create an account"

                        alertMessage = error.localizedDescription

                        showAlertView.toggle()

                    }

                } else {

                    showHomeView.toggle()

                }

            }

        }

    }

**Persistance **

import CoreData



struct PersistenceController {

    static let shared = PersistenceController()

    let container: NSPersistentCloudKitContainer

    init(inMemory: Bool = false) {

        container = NSPersistentCloudKitContainer(name: "ChuckleApp")

        container.viewContext.automaticallyMergesChangesFromParent = true

        if inMemory {

            container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")

        }

        container.loadPersistentStores(completionHandler: { (storeDescription, error) in

            if let error = error as NSError? {

                print("Unresolved error \(error), \(error.userInfo)")

            } else {

                print("Store has been loaded")

            }

        })

    }

}

Main

import SwiftUI

import Firebase

import CoreData



@main

struct ChuckleAppApp: App {

    let persistenceController = PersistenceController.shared

    

    init() {

            FirebaseApp.configure()

        }





    var body: some Scene {

        WindowGroup {

            SignupView()

                .environment(\.managedObjectContext, persistenceController.container.viewContext)

        }

    }

}

not sure of this, but what thread does Auth.auth().addStateDidChangeListener {} call into? Inside that, you're using the viewContext, which must used be on the main thread.

CoreData: Operation could not be completed 'Foundation_GenericObjError error 0'
 
 
Q