"This NSPersistentStoreCoordinator has no persistent stores", despite setting up all capabilities

Hi guys,

First of all, I'm sorry if this is the wrong place to post this. I'm in the last steps of my task manager app: getting the tasks to sync between devices. However, I get the error "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation." What does this error exactly mean? My container is initialised so it should have a persistent store, right? I've also enabled all the proper capabilities I'm pretty sure (eg, I've enabled CloudKit, created a container, enabled background fetch and remote notifications.) Here is the code for my data controller:

import CoreData
import Foundation

class DataController: ObservableObject {
    let container = NSPersistentCloudKitContainer(name: "TaskDataModel")
    
    init() {
        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("Container descriptions not loaded")
        }
        description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
        container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
        container.viewContext.automaticallyMergesChangesFromParent = true
        container.loadPersistentStores { description, error in
            if let error = error {
                print("Core Data failed to load: \(error.localizedDescription)")
            }
        }
    }
}

Here is TaskManMain:

@main
struct TaskManApp: App {
    @StateObject private var dataController = DataController()
    
    var body: some Scene {
        WindowGroup {
            MainView()
                .environment(\.managedObjectContext, dataController.container.viewContext)
        }
    }
}

Here is the full repo if y'all are interested: https://github.com/aabagdi/TaskMan

Thanks for any help!

I get the error "This NSPersistentStoreCoordinator has no persistent stores (unknown). It cannot perform a save operation." What does this error exactly mean?

The error message says the persistent store coordinator has no persistent stores. Core Data saves data to a persistent store. If there are no persistent stores, Core Data cannot save your app's data.

My container is initialised so it should have a persistent store, right?

Are you sure the container was initialized correctly?

let container = NSPersistentCloudKitContainer(name: "TaskDataModel")

Do you have a CloudKit container named TaskDataModel?

Set a breakpoint at the start of the init function in DataController. Does the container property have the value you expect?

Step through the code line by line. Does the call to loadPersistentStores run correctly or does it generate an error?

"This NSPersistentStoreCoordinator has no persistent stores", despite setting up all capabilities
 
 
Q