核心数据+ CloudKit-iOS和watchOS配套应用程序之间的共享 [英] Core data + CloudKit - sharing between iOS and watchOS companion app

查看:51
本文介绍了核心数据+ CloudKit-iOS和watchOS配套应用程序之间的共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将数据与核心数据一起存储.最近,我发现了Apple在WWDC19中引入的新功能,该功能允许核心数据与CloudKit一起使用.我刚刚为我的应用启用了cloudKit,并使用NSPersistentCloudKitContainer而不是NSPersistentContainer,所有设置都已设置!我所有的数据都在ios设备之间共享.它的工作方式类似于NSPersistentContainer,但它在icloud服务器上发送更改的副本,因此始终存在本地数据缓存.

In my app I store data with core data. Recently I discovered the new feature introduced by Apple in WWDC19 which allows core data to work with CloudKit. I just enabled cloudKit for my app and used an NSPersistentCloudKitContainer instead of NSPersistentContainer and all was set up ! All my data is shared between ios devices. That works like NSPersistentContainer but it sends a copy of changes on icloud server, so there is always a local cache of data.

现在,我想从我的Apple Watch伴侣应用中访问该数据,但不是所有数据,而是一个特定的实体!那我该怎么办呢?我试图通过属性检查器设置两个目标之间共享的NSPersistentCloudKitContainer,但注意没有得到任何数据.我可以在cloudKit仪表板中看到watchOS发出的请求,但观看只是没有任何数据.

Now I'd like to access that data from my apple watch companion app but not all the data, only a specific entity! So how could I do that? I tried to set the NSPersistentCloudKitContainer shared between both targets with the attribut inspector but watch don't get any data. I can see in cloudKit dashboard there are requests from watchOS but watch just don't get any data.

但是,如果我将手表中的实体保存到核心数据中,则只能从手表中获取它.我的结论是,两者都没有将数据存储在同一位置.那我该如何解决呢?已经使用了相同的NSPersistendCloudKitContainer.

But if I save an entity from the watch to core data I can get it only from the watch. My conclusion is both are no storing the data at the same place. So how could I fix that? There are already using the same NSPersistendCloudKitContainer.

两个目标之间共享的容器:

the container shared between both targets :

import Foundation
import CoreData

public class CoreDataContainer {
    lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container, having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentCloudKitContainer(name: "MyProjectName")
        container.loadPersistentStores(completionHandler: { (storeDescription, error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist, cannot be created, or disallows writing.
                 * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()

    // MARK: - Core Data Saving support

    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
            }
        }
    }
}

推荐答案

通过在我的上下文描述中添加一个cloudKitContainerOptions来解决:

Resolved by adding a cloudKitContainerOptions to my context description like this :

class CoreDataStack {
    static let persistentContainer: NSPersistentCloudKitContainer = {
        let container = NSPersistentCloudKitContainer(name: "MyProjectName")
        let description = container.persistentStoreDescriptions.first

        description?.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "myCloudContainerID") // HERE !

        container.loadPersistentStores(completionHandler: { (_, error) in
            if let error = error as NSError? {
                fatalError("Unresolved error \(error), \(error.userInfo)")
            }
        })
        return container
    }()  

}

这篇关于核心数据+ CloudKit-iOS和watchOS配套应用程序之间的共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆