gpt4 book ai didi

ios - 无法从 CloudKit notificationInfo 和 userInfo 结构中获取 CKRecord.ID

转载 作者:行者123 更新时间:2023-11-28 10:33:58 25 4
gpt4 key购买 nike

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

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:

[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";
};
}]

最佳答案

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

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)")
}
}

关于ios - 无法从 CloudKit notificationInfo 和 userInfo 结构中获取 CKRecord.ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54612349/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com