2015-12-21 85 views
3

我使用FireOnCreation將訂閱添加到Cloud Kit記錄類型。CloudKit在創建記錄時接收多個通知

在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") 
    } 
} 

回答

3

您可能在這些設備上有多個訂閱。當您創建新訂閱時,請刪除舊訂閱。

看看

CKDatabase.fetchAllSubscriptionsWithCompletionHandler(_:) 

對於一個快速和骯髒的修復,我相信你可以從設備中取出應用程序並重新安裝。

+1

感謝您的回覆。這正是我忽略要做的。我認爲從Cloud Dashboard中刪除訂閱就足夠了。顯然它不是,我發現雲容器很髒。最後,我必須重置開發環境。現在,我使用CKModifySubscriptionsOperation方法刪除舊的並保存新的訂閱。我使用fetchAllSubscriptionsWithCompletionHandler來獲取所有要刪除的當前訂閱。 – HioChao