【发布时间】:2023-04-07 21:46:01
【问题描述】:
我找不到正确的方法来检查 CKRecord 订阅是否已经存在,如果不存在则订阅它以获取推送通知。
我已经实现了订阅本身并且它正在响应,但是每次我输入正确的视图控制器时,我总是试图再次订阅,如果该订阅已经存在,服务器会回复错误 - 我的问题是:有有什么方法可以先检查订阅是否存在,而不是尝试创建它并等待服务器响应?
这是我订阅记录的方式:
// 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
}
}
谢谢
【问题讨论】: