【问题标题】:getting array of child values from firebase database从 firebase 数据库中获取子值数组
【发布时间】:2019-06-21 10:24:26
【问题描述】:

数据库结构

我正在尝试从评论中获取一组用户(字符串),但由于它在我的数据库中有点深,所以遇到了麻烦,这是我尝试过的实现,它返回 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)

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:
    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 ?? "")
                    }
    

    【讨论】:

    • 只有代码的答案通常没有帮助。包含一些关于代码的作用或它如何解决问题中提出的问题的信息是一个好主意。在这种情况下,它与问题中提出的 Firebase 结构不匹配。这是一个解决方案吗?一个额外的问题?还有什么?
    猜你喜欢
    • 2017-10-13
    • 2019-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 2020-09-04
    • 2017-11-09
    相关资源
    最近更新 更多