ios - 从firebase数据库获取子值数组
问题描述
数据库结构
我正在尝试从评论中获取一组用户(字符串),但是由于它在我的数据库中有点深,所以遇到了麻烦,这是我尝试过的实现,它返回 0 作为计数。
REF_FEEDMESSAGES.child(messageKey).child("comments").observeSingleEvent(of: .value) { (commentSnapshot) in
guard let commentSnapshot = commentSnapshot.children.allObjects as? [DataSnapshot] else {return}
for comment in commentSnapshot {
let theComment = comment.value as? [String: Any]
let theContent = theComment?["content"] as? String ?? ""
let theIcon = theComment?["icon"] as? String ?? ""
let theColor = theComment?["color"] as? String ?? ""
let theDate = theComment?["date"] as? String ?? "0"
let theName = theComment?["userName"] as? String ?? ""
let theVerified = theComment?["isVerified"] as? String ?? "no"
let profileImageURL = theComment?["profileImageURL"] as? String ?? ""
let postedBy = theComment?["postedBy"] as? String ?? ""
let likes = theComment?["likes"] as? String ?? ""
let key = comment.key
let likers = comment.childSnapshot(forPath: "likedBy").value as? [String] ?? []
print(likers.count)
解决方案
guard let likers = comment.childSnapshot(forPath: "likedBy").children.allObjects as? [DataSnapshot] else {return}
for like in likers {
let theLike = like.value as? [String:Any]
print(theLike!["user"] as? String ?? "")
commentLiked.append(theLike!["user"] as? String ?? "")
}
推荐阅读
- keycloak - 如何在 keycloak 4.6 中排除 url?
- cassandra - Cassandra 数据分布
- javascript - 为什么 if/then 对 document.querySelector 不起作用?Rockcissorpaper 游戏的部分代码
- php - 如何将 base64 编码图像上传到 S3 存储桶 php codeigniter
- sql - 尝试从访问表中删除重复记录
- json - 在 ResponseEntity 中发送变量
- azure - Azure Log Analytics 通过工作区 id 和 PRIMARY KEY 使用 rest api 查询/获取数据
- javascript - 如何将javascript(正则表达式)字符串与另一个忽略字符串顺序的字符串进行比较/匹配/替换
- arrays - 无法从解析的 JSON 中获取字段值
- java - 问题数组到文件中