如何解决CoreData + CloudKit-使用NotificationCenter和AppDelegate收听远程通知之间的区别?
我发现了两种方法来将我的CoreData存储与CloudKit同步。一个使用NotificationCenter
,另一个使用AppDelegate
函数。
我的问题是两者之间是否有区别?我发现使用didReceiveRemoteNotification
和didRegisterForRemoteNotificationsWithDeviceToken
的同步速度更快?
更多详细信息:
在我的CoreDataStack中使用NotificationCenter
:
var persistentContainer: NSPersistentCloudKitContainer = {
...
NotificationCenter.default.addObserver(self,selector: #selector(self.processUpdate),name: .NSPersistentStoreRemoteChange,object: container.persistentStoreCoordinator)
}()
使用AppDelegate
func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
application.registerForRemoteNotifications()
return true
}
func application(_ application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
processUpdate()
}
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable : Any],fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
processUpdate()
}
两者都:
@objc func processUpdate() { ... } //Here I fetch from CoreData and update UI
谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。