Swift CoreData&x2B;CloudKit仅在最小化并重新打开应用程序后同步

Swift CoreData&x2B;CloudKit仅在最小化并重新打开应用程序后同步,swift,core-data,cloudkit,Swift,Core Data,Cloudkit,我目前正在制作一个应用程序,它需要核心数据存储通过iCloud跨设备同步 除了不能实时同步之外,一切正常。(或者至少有点接近这一点)。当我在Device1更改某些内容后在Device2上什么也不做时,它根本不同步。事实上,我必须最小化并重新打开Device2上的应用程序才能使同步工作正常 这很好地说明了这一点:() 下面是我的代码片段: let container: NSPersistentCloudKitContainer var context: NSManagedObjectContext

我目前正在制作一个应用程序,它需要核心数据存储通过iCloud跨设备同步

除了不能实时同步之外,一切正常。(或者至少有点接近这一点)。当我在Device1更改某些内容后在Device2上什么也不做时,它根本不同步。事实上,我必须最小化并重新打开Device2上的应用程序才能使同步工作正常

这很好地说明了这一点:()

下面是我的代码片段:

let container: NSPersistentCloudKitContainer
var context: NSManagedObjectContext { container.viewContext }

init() {
    container = NSPersistentCloudKitContainer(name: "__Decision")
    
    guard let description = container.persistentStoreDescriptions.first else { ... }
    description.setOption(true as NSObject, forKey:
        NSPersistentStoreRemoteChangeNotificationPostOptionKey)
    
    container.loadPersistentStores(completionHandler: { ... })
    
    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
    
    NotificationCenter.default.addObserver(self, selector: #selector(self.processUpdate), name: .NSPersistentStoreRemoteChange, object: nil)
}


@objc func processUpdate(notification: NSNotification) {
    operationQueue.addOperation {
        DispatchQueue.main.async { ... } //Fetch new synched data and update UI 
    }
}
    
    
lazy var operationQueue: OperationQueue = {
    var queue = OperationQueue()
    queue.maxConcurrentOperationCount = 1
    return queue
}()
由于这是我第一个使用CloudKit+CoreData的项目,所以我遵循了apple的文档和其他教程

一切似乎都和他们所做的一模一样。。我不知道,从几天以来一直在研究这个问题


谢谢大家!

我只是向您展示了我的设置,因为我曾经遇到过同样的问题。我也在使用CloudKit进行macOS和iOS同步,现在确实可以了。使用SwiftUI更新视图

在我的AppDelegate中,我首先注册remoteNotification

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    application.registerForRemoteNotifications()
    
    return true
}
我的persistenContainer看起来确实像你的:

lazy var persistentContainer: NSPersistentCloudKitContainer = {
    let container = NSPersistentCloudKitContainer(name: "name")
    
    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
        if let error = error as NSError? {
            fatalError("Unresolved error \(error), \(error.userInfo)")
        }
    })
    
    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyStoreTrumpMergePolicy
    
    return container
}()
然后是我的签名选项卡,添加背景模式:

。。并添加推送和iCloud


还有,要有耐心。有时同步确实需要几分钟才能更新。

即使关闭和打开应用程序,我也遇到了完全相同的问题。我也有同样的问题。我再次按照所有说明进行操作,确保设置了远程通知,并且成功了。您的代码看起来很好,可能是设置HM,那么我也应该这样做,谢谢您的帮助。请尝试调用
application.registerforremotentifications()
如果您通过
description.cloudKitContainerOptions?.databaseScope=.public将NSPersistentStoreDescription设置为与公共数据库交互,而不是与私有数据库交互(我相信是iOS 14之前的默认设置),请确保在您的didFinishLaunchingWithOptions?中添加PushNotifications,它只会按设计每隔30分钟对cloudkit的更改进行一次投票(请参阅WWDC 2020课程视频中关于此主题的10:30:developer.apple.com/videos/play/wwdc2020/10650)。嘿,再次感谢您的帮助。不知怎的,它现在起作用了!!我不知道
应用程序。registerforremotentifications()
,但我已经做了:-清理了构建文件夹。-已从我的所有设备中删除该应用程序。-完全删除了当前的应用程序iCloud数据。然后它又起作用了。。。我之所以这么做,是因为我为CoreData和CloudKit制作了一个简单的虚拟应用程序,它的代码完全相同,而且很有效。。。