如何解决来自公共 NSPersistentCloudKitContainer 的记录在几秒钟内同步到 iCloud,但仅在启动时来自物理设备?
我目前正在开发的 iPhone 应用程序适用于 iOS >= 14.0 使用 AppDelegate 中的以下命令将大部分应用程序记录共享到公共 iCloud 容器:
lazy var persistentContainerPublic: NSPersistentCloudKitContainer = {
let container = NSPersistentCloudKitContainer(name: "PublicModel")
guard let description = container.persistentStoreDescriptions.first else {
print("Can't get description")
fatalError("\(#function): ERROR")
}
description.setoption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey) // https://developer.apple.com/videos/play/wwdc2020/10650
description.setoption(true as NSNumber,forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey) // https://developer.apple.com/videos/play/wwdc2020/10650
description.cloudKitContainerOptions?.databaseScope = .public
container.loadPersistentStores(completionHandler: { (storeDescription,error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error),\(error.userInfo)")
} else {
print("\(type(of: self)) - loadPersistentStores completed sucessful")
}
})
return container
}()
记录会在几秒钟内同步到 iCloud 容器,但仅在启动时同步到物理设备,有时我使用以下代码获取新记录:
private var pullControl = UIRefreshControl()
pullControl.attributedTitle = NSAttributedString(string: "")
pullControl.addTarget(self,action: #selector(refreshListData(_:)),for: .valueChanged)
tableView.refreshControl = pullControl
@objc private func refreshListData(_ sender: Any) {
fetchDocuments() // fetch all records from core data
tableView.reloadData()
refreshPublicObjects()
self.pullControl.endRefreshing()
}
这里是 refreshPublicObjects 函数:
func refreshPublicObjects () {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let persistentContainer = appDelegate?.persistentContainerPublic
persistentContainer?.viewContext.refreshAllObjects()
}
我是否缺少从 iCloud 及时获取更新的任何内容,不仅在启动时,甚至用户没有刷新表视图?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。