使用Codable和CodableFirebase快速解析Firebase值 [英] Getting nil in parsing Firebase values swift using Codable and CodableFirebase
问题描述
我正在使用Firebase实时数据库,并在快速库和外部库CodableFirebase中使用了编码方法.我已经创建了模型结构,但是当我尝试使用模型结构解析值(因为我获取所有值)时,结果为零.我的数据库具有可能无法正确处理嵌套值的键.请帮忙.谢谢
I am using Firebase Realtime Database, using codable approach in swift and external library CodableFirebase. I have created model structure but when I am trying to parse values (as i am getting all values) with model structure it gives me nil. My database has keys which might I am not properly handling in nested values. Please help. Thanks
附加了数据库结构快照.
database structure snapshot attached.
代码:
Database.database().reference().child("users").observeSingleEvent(of: .value, with: { (snapshot) in
guard let value = snapshot.value as? [String: Any] else { return }
do {
let friendList = try FirebaseDecoder().decode(Response.self, from: value)
guard let conversationUid = value["conversationUid"] as? String,
let friendStatus = value["friendStatus"] as? String,
let notify = value["notify"] as? Bool,
let phNumber = value["phoneNumber"] as? String,
let uid = value["uid"] as? String
else { return }
} catch let error {
print(error)
}
})
JSON:
{
"FTgzbZ9uWBTkiZK9kqLZaAIhEDv1" : {
"friends" : {
"zzV6DQSXUyUkPHgENDbEjXVBj2" : {
"conversationUid" : "-L_w2yi8gh49GppDP3r5",
"friendStatus" : "STATUS_ACCEPTED",
"notify" : true,
"phoneNumber" : "+9053",
"uid" : "zzV6DQSXUyUkPHgEZ9EjXVBj2"
}
},
"lastLocation" : {
"batteryStatus" : 22,
"latitude" : 48.90537,
"longitude" : 28.042,
"timeStamp" : 1556568633477,
"uid" : "FTgzbZ9uWkiZK9kqLZaAIhEDv1"
},
"profile" : {
"fcmToken" : "fp09-Y9ZAkQ:APA91bFgGBsyFx0rtrz7roxzpE_MmuSaMc4is-XIu7j718qjRVCSHY4PvbNjL1LZ-iytaeDKviIRMH",
"name" : "Mt Bt",
"phoneNumber" : "+90503",
"uid" : "FTgzbZ9uWBTkiZLZaAIhEDv1"
}
}
型号:
struct Response : Codable {
let friends : Friend?
let lastLocation : LastLocation?
let profile : Profile?
}
struct Friend: Codable {
let converstionUid: String?
let friendStatus: String?
let notify: Bool?
let phoneNumber: String?
let uid: String?
}
struct Profile : Codable {
let fcmToken : String?
let name : String?
let phoneNumber : String?
let uid : String?
}
struct LastLocation : Codable {
let batteryStatus : Int?
let latitude : Float?
let longitude : Float?
let timeStamp : Int?
let uid : String?
}
推荐答案
您的代码正在读取整个 users
节点,然后尝试读取 conversationUid
和其他属性从那个节点.由于这些属性在 users
节点下不直接存在,因此将为空.
Your code is reading the entire users
node, and then tries to read the conversationUid
and other properties from that node. Since these properties don't exist directly under the users
node, you get null.
要正确解析此JSON,您需要先浏览三个级别的子节点,然后再尝试读取诸如 conversationUid
之类的命名属性.
To properly parse this JSON, you'll need to navigate the three levels of child nodes before you try to read the named properties like conversationUid
.
Database.database().reference().child("users").observeSingleEvent(of: .value, with: { (snapshot) in
for userSnapshot in snapshot.children.allObjects as! [DataSnapshot] {
let friendsSnapshot = userSnapshot.childSnapshot(forPath: "friends")
for friendSnapshot in friendsSnapshot.children.allObjects as! [DataSnapshot] {
guard let value = friendSnapshot.value as? [String: Any] else { return }
do {
guard let conversationUid = value["conversationUid"] as? String,
...
上面的代码首先循环遍历/users
下的第一级子节点,然后为每个用户遍历 friends
节点的子级.
The above code first loops over the first-level child nodes under /users
, and it then loops over the children of the friends
node for each user.
这篇关于使用Codable和CodableFirebase快速解析Firebase值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!