我已经设法让 CoreData 与 CloudKit 一起使用我的应用程序的 iOS 版本上的新 NSPersistentCloudKitContainer 并让它在应用程序运行时自动同步。但是,当我在 watchOS 应用程序上进行设置时,我注意到只有在我强制关闭并重新打开手表应用程序时才会发生同步。
import Foundation
import CoreData
class DataManager : NSObject {
static let shared = DataManager()
#if os(watchOS)
let transactionAuthorName = "watchOSApp"
#else
let transactionAuthorName = "iOSApp"
#endif
override private init() {
super.init()
}
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "Model")
let cloudStoreUrl = applicationDocumentDirectory()!.appendingPathComponent("product.sqlite")
let cloudStoreDescription = NSPersistentStoreDescription(url: cloudStoreUrl)
cloudStoreDescription.shouldInferMappingModelAutomatically = true
cloudStoreDescription.shouldMigrateStoreAutomatically = true
cloudStoreDescription.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier:"iCloud.com.company.product")
container.persistentStoreDescriptions = [cloudStoreDescription]
container.loadPersistentStores(completionHandler: { storeDescription, error in …Run Code Online (Sandbox Code Playgroud) core-data ios cloudkit watchos-6 nspersistentcloudkitcontainer