UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Trouble getting CloudKit to work with Core Data

Forums > Swift

Hi all, I followed the tutorial on Apple's website (https://developer.apple.com/documentation/coredata/mirroring_a_core_data_store_with_cloudkit/setting_up_core_data_with_cloudkit), but for some reason the persistent store is failing to load. I've tried looking around for a clear answer, but to no avail, it's starting to drive me crazy lmao. The store was working before I applied the capabilities too, which is even more confusing. 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)")

My CoreData model is called "TaskDataModel", but my CloudKit container has a different name. Could that be cause for a problem? My full repo is here: https://github.com/aabagdi/TaskMan

Thank you for any help!


Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.