2
我找不到合適的正確方法來檢查CKRecord訂閱是否已經存在,如果訂閱不存在,則訂閱它以獲取推送通知。檢查記錄訂閱是否已在CloudKit中存在
我已經實現了訂閱本身,它的響應,但每次我進入正確的視圖控制器時,我總是試圖再次訂閱和服務器回覆一個錯誤,如果該訂閱已經存在 - 我的問題是:是有沒有辦法檢查訂閱是否存在,而不是嘗試創建它並等待服務器響應?
這裏是我如何訂閱記錄:
// 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
}
}
感謝