从Firebase获取多级节点

时间:2018-09-23 10:12:24

标签: swift firebase firebase-realtime-database

我正在尝试从节点中获取“朋友”,以便以后在UICollectionView中显示他们。现在,我意识到我必须使用一种结构,并将Friends数组放入其中。我现在正在努力了解如何将它们提取到该数组中(您可以在文章底部看到它)。数据存储在Firebase节点中。我如何获取数据,之后如何将其放入UICollectionView中?到目前为止,这是我要检索的功能。

更新:(我认为我现在正在正确地获取数据,但没有得到任何结果。在集合视图中我应该做些什么吗?还是我做错了什么?)

更新:这是我的帖子提取代码:

func fetchPosts3() {

    ref.child("Users_Posts").child("\(unique)").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
        print(snapshot)

        if snapshot.value as? [String : AnyObject] != nil {

            let allPosts = snapshot.value as! [String : AnyObject]
            self.posts.removeAll()

            for (_, value) in allPosts {

                if  let postID = value["postID"] as? String,
                    let userIDDD = value["userID"] as? String

                {
                    //ACCESS FRIENDS
                    ref.child("Users_Posts").child("\(unique)").child(postID).child("friends").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
                        print("FRIENDS: \(snap.childrenCount)")

                        //var routine = self.postsWithFriends[0].friends
                        for friendSnap in snap.children {
                            if let friendSnapshot = friendSnap as? DataSnapshot  {

                                let friendDict = friendSnapshot.value as? [String: Any]
                                let friendName = friendDict?["name"] as? String
                                let friendPostID = friendDict?["postID"] as? String

                                let postsToShow = PostWithFriends(id: userIDDD, friends: [Friend(friendName: friendName!, friendPostID: friendPostID!)])

                                self.postsWithFriends.append(postsToShow)
                                print("COUNTING: \(self.postsWithFriends.count)")

                                // then do whatever you need with your friendOnPost
                            }
                        }


                    })
                }
            }
            //GET LOCATION

            self.collectionView?.reloadData()
            self.posts.sort(by: {$0.intervalPosts! > $1.intervalPosts!})


        }
    })

    ref.removeAllObservers()
}

这就是数据如何看待数据库:

{
    "-LN2rl2414KAISO_qcK_" : {
        "cellID" : "2",
        "city" : "Reading",
        "date" : "2018-09-23 00:41:26 +0000",
        "friends" : {
            "UJDB35HDTIdssCtZfEsMbDDmBYw2" : {
                "name" : "Natalia",
                "postID" : "-LN2rl2414KAISO_qcK_",
                "userID" : "UJDB35HDTIdssCtZfEsMbDDmBYw2"
            },
            "Vyobk7hJu5OGzOe7E1fcYTbMvVI2" : {
                "name" : "Gina C",
                "postID" : "-LN2rl2414KAISO_qcK_",
                "userID" : "Vyobk7hJu5OGzOe7E1fcYTbMvVI2"
            }
        },
    }
}

这是我存储在数组中的对象

struct PostWithFriends {
    var postID : String?
    var friends: [Friend]
}

class Friend : NSObject {
    var friendName: String?
    var friendUserID: String?
    var postID: String?

    init(friendName: String, friendPostID: String) {
        self.friendName = friendName
        self.postID = friendPostID
    }
}

1 个答案:

答案 0 :(得分:1)

替换此

if let friend = snap.value as? [String : AnyObject] {

}

与此:

for friendSnap in snap.children {
    if let friendSnapshot = friendSnap as? FIRDataSnapshot  {
        let friendOnPost = FriendOnPost()
        let friendDict = friendSnapshot.value as? [String: Any]
        friendOnPost.name = friendDict?["name"] as? String
        friendOnPost.friendUserID = friendDict?["userID"] as? String
        friendOnPost.postID = friendDict?["postID"] as? String
        // then do whatever you need with your friendOnPost
    }
}