使用Codable和CodableFirebase快速解析Firebase值

编程入门 行业动态 更新时间:2024-10-05 19:15:31
本文介绍了使用Codable和CodableFirebase快速解析Firebase值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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值

本文发布于:2023-11-26 23:27:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快速   Codable   CodableFirebase   Firebase

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!