gpt4 book ai didi

core-data - 核心数据 + CloudKit - 在 iOS 和 watchOS 配套应用之间共享

转载 作者:行者123 更新时间:2023-12-03 09:34:01 25 4
gpt4 key购买 nike

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

现在我想从我的 Apple Watch 配套应用程序访问该数据,但不是所有数据,只有特定实体!
那我怎么能这样做呢?
我尝试使用属性检查器设置在两个目标之间共享的 NSPersistentCloudKitContainer,但看不到任何数据。我可以在 cloudKit 仪表板中看到有来自 watchOS 的请求,但 watch 没有获得任何数据。

但是如果我将 watch 中的实体保存到核心数据中,我只能从 watch 中获取它。
我的结论是两者都没有将数据存储在同一个地方。那么我该如何解决呢?已经有使用相同的 NSPersistendCloudKitContainer。

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

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 来解决,如下所示:

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
}()

}

关于core-data - 核心数据 + CloudKit - 在 iOS 和 watchOS 配套应用之间共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com