• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 检查 CloudKit 中是否已存在记录订阅

[复制链接]
菜鸟教程小白 发表于 2022-12-12 13:34:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我找不到正确的方法来检查 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
    }
}

谢谢



Best Answer-推荐答案


您可以使用 fetchAllSubscriptionsWithCompletionHandler 来查询所有现有的 subs,然后您可以检查完成处理程序中返回的每个 sub 的 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
            }
        }
    }];

但是,您提到重新进入 View Controller 并重新运行此检查。并不是说此检查每次都会向服务器发出请求,因此将计入您的事务和传输配额。我建议您在应用启动时运行一次此检查,然后将每个子的状态保存在类变量中,这样您就不必通过不必要的调用反复重新查询服务器。

关于ios - 检查 CloudKit 中是否已存在记录订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44540026/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap