问题描述
我目前正在开发的 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 (将#修改为@)