我正在使用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")
}
}