我正在使用 FireOnCreation 添加对 Cloud Kit 记录类型的订阅。
在 appDelegate 中,我使用didReceiveRemoteNotificationWithCompletionHandler
来捕获通知。我遇到的问题是,当创建与谓词匹配的记录时,didReceiveRemoteNotificationWithCompletionHandler
会触发多次。在设备 A 上,它会持续触发 3 次,而在设备 B 上,它将触发 6 次。我什至尝试在 Cloud DashBoard 上创建记录,但它仍然会做同样的事情。所以这说明问题不在于记录的创建。任何建议或提示都会有很大帮助。
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
}
func subscribeRecordChangesForCurrentUser(userRecordID: CKRecordID) {
print("subscribeRecordChangesForCurrentUser \(userRecordID.recordName)")
let userRef = CKReference(recordID: userRecordID, action: CKReferenceAction.None)
let predicate = NSPredicate(format: "toUsers CONTAINS %@", userRef)
let subscription = CKSubscription(recordType: "Track", predicate: predicate, options: [.FiresOnRecordCreation])
let notificationInfo = CKNotificationInfo()
notificationInfo.alertBody = "Created a new track."
notificationInfo.shouldSendContentAvailable = true
notificationInfo.soundName = UILocalNotificationDefaultSoundName
subscription.notificationInfo = notificationInfo
let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
publicDatabase.saveSubscription(subscription) { (subscription: CKSubscription?, error: NSError?) -> Void in
guard error == nil else {
print(error?.localizedDescription)
return
}
print("successfully subscript user")
}
}