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

Unable to loadPersistentStores from a NSPersistentCloudKitContainer - WatchOS7 - SwiftUI App Lifecycle

Forums > watchOS

Hi everybody,

I am trying to make my WatchOS app load and manipulate its data from a CloudKit container instead of a local CoreData one but the container.loadPersistentStores fails when I use a NSPersistentCloudKitContainer instead of an NSPersistentContainer.

Here the DataController class I used for my app (same as the one used by Paul for the Ultimate Portfolio app)

class DataController: ObservableObject {
    let container: NSPersistentCloudKitContainer

    init(inMemory: Bool = false) {
        container = NSPersistentCloudKitContainer(name: "Main")

        if inMemory {
            container.persistentStoreDescriptions.first?.url = URL(fileURLWithPath: "/dev/null")
        }

        guard let description = container.persistentStoreDescriptions.first else {
            fatalError("Could not retrieve a persistent store description.")
        }

        print("OK - \(description)")

        container.loadPersistentStores { storeDescription, error in
            if let error = error {
                fatalError("Fatal error loading store: \(error.localizedDescription)")
            }
        }
    }
}

The block:

container.loadPersistentStores { storeDescription, error in
       if let error = error {
            fatalError("Fatal error loading store: \(error.localizedDescription)")
       }
}

contantly fails and give me the error: Fatal error: Fatal error loading store: A Core Data error occurred. which is not very helpful. I tried copy paste the same code in an iOS app and it works, but when I use this code on a WatchOS only app, it crashes.

Is there a limitation for using CloudKit with a WatchOS app? I searched on the internet but there are very few topics on this subject for watchOS.

If you have some knowledge on using CloudKit with WatchOS (especially with SwiftUI lifecycle), that can be useful as there is very few resources.

Thanks for your help.

Alexis

5      

Found it! I put a constraint in the xcdatamodel (unique id) which caused the store to not load properly with NSPersistentCloudKitContainer.

I'll figure out another way to handle unique id

5      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.