检查 CloudKit 中是否已存在记录订阅
Check if record subscription already exists in CloudKit
我找不到正确的方法来检查 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
}
}
谢谢
您可以使用 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
}
}
}];
但是,您提到重新进入视图控制器并重新运行进行此检查。并不是说此检查每次都会向服务器发出请求,因此会计入您的交易和传输配额。相反,我建议您 运行 在应用程序启动时检查一次,然后将每个子的状态保存在 class 变量中,这样您就不必通过不必要的调用反复重新查询服务器。
我找不到正确的方法来检查 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
}
}
谢谢
您可以使用 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
}
}
}];
但是,您提到重新进入视图控制器并重新运行进行此检查。并不是说此检查每次都会向服务器发出请求,因此会计入您的交易和传输配额。相反,我建议您 运行 在应用程序启动时检查一次,然后将每个子的状态保存在 class 变量中,这样您就不必通过不必要的调用反复重新查询服务器。