从firebase数据库获取子值数组

时间:2019-06-21 01:31:53

标签: ios swift firebase firebase-realtime-database

数据库结构

database structure

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

1 个答案:

答案 0 :(得分:-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 ?? "")
                }