簡體   English   中英

檢查記錄訂閱在CloudKit中是否已經存在

[英]Check if record subscription already exists in CloudKit

我找不到正確正確的方法來檢查CKRecord訂閱是否已經存在,以及是否不訂閱它來獲取推送通知。

我已經實現了訂閱本身,並且正在響應,但是每次我輸入正確的View Controller時,我總是嘗試再次訂閱,並且如果該訂閱已經存在,服務器將返回錯誤-我的問題是:有沒有辦法首先檢查訂閱是否存在,而不是嘗試創建它並等待服務器響應?

這是我訂閱記錄的方式:

    // Create the predicate
let predicate = NSPredicate(format: "recordId = %@", RECORD_ID)

// Create a subscription specifying the record type, predicate and notification options
let subscription = CKQuerySubscription(recordType: "Tabs", predicate: predicate, options: [.firesOnRecordUpdate, .firesOnRecordCreation])

// Create a CloudKit notification object
let notificationInfo = CKNotificationInfo()
notificationInfo.alertLocalizationKey = "Updates have been made"
notificationInfo.shouldBadge = true

// Set the subscriptor's notification object to the new CloudKit notification object
subscription.notificationInfo = notificationInfo

// Save the subscription to the database
let publicDatabase = CKContainer.default().publicCloudDatabase
publicDatabase.save(subscription) { (subs, err) in
    if let err = err {
        print("Failed to save subscription:", err)
        return
    }
}

謝謝

您可以使用fetchAllSubscriptionsWithCompletionHandler來查詢所有現有子項,然后可以在完成處理程序中返回的每個子項上檢查subscriptionID屬性。 Objective-C版本如下所示:

    [publicDatabase fetchAllSubscriptionsWithCompletionHandler:^(NSArray<CKSubscription *> * _Nullable subscriptions, NSError * _Nullable error)
    {
        NSMutableArray *subIDs = [NSMutableArray new];
        for (CKSubscription *sub in subscriptions)
        {
            if ([sub.subscriptionID isEqualToString:@"whatever"];
            {
                  //do some stuff
            }
        }
    }];

但是,您提到重新輸入視圖控制器並重新運行此檢查。 並不是說此檢查每次都會向服務器發出請求,因此將計入您的交易和轉移配額。 相反,我建議您在應用程序啟動時運行一次此檢查,然后將每個子項的狀態保存在類變量中,這樣就不必繼續通過不必要的調用來反復重新查詢服務器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM