3

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

1 回答 1

3

您可能在这些设备上有多个订阅。创建新订阅时,请删除旧订阅。

看一眼

CKDatabase.fetchAllSubscriptionsWithCompletionHandler(_:)

为了快速而肮脏的修复,我相信您可以从设备中删除该应用程序并重新安装它。

于 2015-12-21T05:37:42.173 回答