получение массива дочерних значений из базы данных Firebase - PullRequest
0 голосов
/ 21 июня 2019

структура базы данных

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 голосов
/ 21 июня 2019
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 ?? "")
                }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...