我通过将 userInfo 转换为 CKDatabaseNotification 来处理私有和共享数据库通知。但是我确实在 didReceiveRemoteNotification 方法中也收到了公共数据库通知,并且 Apple 模板代码没有显示如何处理它并且它引发了一个 fatalError。如何通过我的 fetchChanges 方法处理公共数据库通知?
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
let dict = userInfo as! [String: NSObject]
guard let vc = self.window?.rootViewController as? UIViewController else { return }
guard let notification:CKDatabaseNotification = CKNotification(fromRemoteNotificationDictionary:dict) as? CKDatabaseNotification else { return }
self.fetchChanges(in: notification.databaseScope) {
completionHandler(UIBackgroundFetchResult.newData)
}
}
func fetchChanges(in databaseScope: CKDatabaseScope, completion: @escaping () -> Void) {
switch databaseScope {
case .private:
self.fetchPrivateChanges(completion: completion)
case .shared:
self.fetchSharedChanges(completion:) { status in
if (status == false) {
return
}
}
case .public:
fatalError()
}
}