首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法从CKRecord.ID notificationInfo和userInfo结构中获取userInfo

无法从CKRecord.ID notificationInfo和userInfo结构中获取userInfo
EN

Stack Overflow用户
提问于 2019-02-10 00:53:29
回答 1查看 253关注 0票数 1

我已经在公共数据库上为CloudKit应用程序设置了订阅。我想从返回的recordName中检索notificationInfo。我还没有找到任何文档来解释如何使用框架方法检索recordName,所以我尝试手动解析该信息--它不起作用,但这似乎是一种严重的蛮力,我不确定我是否可以指望notificationInfo结构始终是相同的。如有任何指导,将不胜感激。这是代码片段。

代码语言:javascript
复制
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

    var changedCKRecord : String

    let dict = userInfo as! [String : NSObject]
    let notification = CKNotification(fromRemoteNotificationDictionary: dict)
    let db = CloudKitDatabase.shared

    if notification.subscriptionID == db.ckStyleSubscriptionID {
        db.handleNotification()
        completionHandler(.newData)
        print(".newData for CKStyle returned")

        for (key,value) in dict {
            if key == "ck" {
                let newDict : [String : NSObject] = value as! [String : NSObject]
                for (key2,value2) in newDict {
                    if key2 == "qry" {
                        let thirdDict : [String : NSObject] = value2 as! [String : NSObject]
                        for (key3,value3) in thirdDict {
                            if key3 == "rid" {
                                if let end = value3 as? String {
                                    changedCKRecord = end
                                    print(changedCKRecord as Any)
                                }
                            }//if key3
                        }//for key3
                    }//if key2
                }//for key 2 in
            }//if key is ck
        }//for in

    } else {
        completionHandler(.noData)
        print(notification.subscriptionID as Any)
        print(db.ckStyleSubscriptionID)
        print(".noData for CKStyle returned")
    }//if notification else

}//didReceiveRemoteNotification

如果我打印notificationInfo:

代码语言:javascript
复制
[AnyHashable("aps"): {"content-available" = 1;},
AnyHashable("ck"): {
ce = 2;
cid = "iCloud. the real bundle identifier  ";
ckuserid = "_c4478d4d3a212da39cca27eb17dffe03";
nid = "58372f81-d10b-4e3b-98ae-40a0d1e048a2";
qry =     {
dbs = 2;
fo = 2;
rid = "97DB306F-3277-07A1-04F5-40228A5EF036";
sid = "ckstyle-changes";
zid = "_defaultZone";
zoid = "_defaultOwner";
};
}]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-10 06:31:05

如果通知是订阅的结果,那么CKNotification(fromRemoteNotificationDictionary:)实际上将返回一个CKQueryNotification实例。然后,您可以访问CKQueryNotificationrecordID属性以获得更改的记录的id。

代码语言:javascript
复制
let dict = userInfo as! [String : NSObject]
let notification = CKNotification(fromRemoteNotificationDictionary: dict)
if let queryNotification = notification as? CKQueryNotification,
   let recordName = queryNotification.recordID?.recordName {
       print("Record name is \(recordName)")
   }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54612349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档