代码之家  ›  专栏  ›  技术社区  ›  Marian Petrisor

从Firebase获取多级节点

  •  0
  • Marian Petrisor  · 技术社区  · 7 年前

    我正在尝试从节点获取“朋友”,以便能够在中显示他们 UICollectionView 之后。我现在意识到我必须使用一个struct并将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 回复  |  直到 7 年前
        1
  •  1
  •   Yury Imashev    7 年前

    替换此

    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
        }
    }
    
    推荐文章