如何解决从CloudKit + CoreData触发了多个重复的NSPersistentStoreRemoteChange通知
我看到.NSPersistentStoreRemoteChange
条通知被多次接收,有时多达10次。
我不认为这是有害的,但是最好的情况是它会消耗处理能力。
所以我的问题是:
- 这有害吗?
- 我可以预防吗?
- 如果不是,是否有建议的方法来忽略重复的通知?
-
我有以下代码来设置我的容器。这包含在一个单例的初始化程序中,我已经确认它被调用过一次。
guard let modelURL = Bundle(for: type(of: self)).url(forResource: momdName,withExtension:"momd"),let mom = NSManagedObjectModel(contentsOf: modelURL)
else {
fatalError("? Error loading model from bundle")
}
let containerURL = folderToStoreDatabaseIn.appendingPathComponent("Model.sqlite")
container = NSPersistentCloudKitContainer(name: momdName,managedObjectModel: mom)
guard let description = container.persistentStoreDescriptions.first else {
fatalError("? ###\(#function): Failed to retrieve a persistent store description.")
}
description.url = containerURL
description.setOption(true as NSNumber,forKey: NSPersistentHistoryTrackingKey)
description.setOption(true as NSNumber,forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
super.init()
// this must be called after super.init()
// ***** ADD OBSERVER *****
NotificationCenter.default.addObserver(self,selector: #selector(updatedFromCKCD(_:)),name: .NSPersistentStoreRemoteChange,object: container.persistentStoreCoordinator)
if let tokenData = try? Data(contentsOf: tokenFile) {
do {
lastToken = try NSKeyedUnarchiver.unarchivedObject(ofClass: NSPersistentHistoryToken.self,from: tokenData)
} catch {
Logger.error("? ###\(#function): Failed to unarchive NSPersistentHistoryToken. Error = \(error)")
}
}
处理这些更改的代码:
// https://developer.apple.com/documentation/coredata/consuming_relevant_store_changes
@objc func updatedFromCKCD(_ notifiction: Notification) {
let fetchHistoryRequest = NSPersistentHistoryChangeRequest.fetchHistory(
after: lastToken
)
let context = container.newBackgroundContext()
guard
let historyResult = try? context.execute(fetchHistoryRequest)
as? NSPersistentHistoryResult,let history = historyResult.result as? [NSPersistentHistoryTransaction]
else {
Logger.error("⛈ Could not convert history result to transactions")
assertionFailure()
return
}
Logger.debug("⛈ Found cloud changes since: \(self.lastToken?.description ?? "nil")")
DispatchQueue.main.async {
// look for particular set of changes which require the user to take action
...
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。