按下按钮时如何添加谷歌地图标记?

时间:2017-09-14 02:05:54

标签: ios swift firebase firebase-realtime-database

是否可以从firebase db中的父级获取所有子节点?

试着这样说:this

我想同时收到所有帖子。

它只提取视频,但只提取当前用户的视频。我想同时获取所有用户的视频。 这里有一些代码可以更好地理解我正在做的事情:

fileprivate func fetchAllPost() {
    fetchPosts()
    fetchAllPostsFromUserIds()
}

fileprivate func fetchAllPostsFromUserIds() {
    guard let uid = FIRAuth.auth()?.currentUser?.uid else { return }
    FIRDatabase.database().reference().child("posts").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

        guard let userIdsDictionary = snapshot.value as? [String: Any] else { return }
        userIdsDictionary.forEach({ (key,  value) in
            FIRDatabase.fetchUserWithUid(uid: key, completion: { (user) in
                self.fetchPostsWithUser(user: user)
            })
        })
    }) { (err) in
        print("failed to fetch following users ids:", err)
    }
}

var posts = [Post]()
fileprivate func fetchPosts() {
    guard let currentUserID = FIRAuth.auth()?.currentUser?.uid else { return }
    FIRDatabase.fetchUserWithUid(uid: currentUserID) { (user) in
        self.fetchPostsWithUser(user: user)
    }
}

fileprivate func fetchPostsWithUser(user: User) {
    let ref = FIRDatabase.database().reference().child("posts/\(user.uid)/")

    ref.observeSingleEvent(of: .value, with: { (snapshot) in

        self.collectionView?.refreshControl?.endRefreshing()

        guard let dictionaries = snapshot.value as? [String: Any] else { return }
        dictionaries.forEach({ (key,value) in

            guard let dictionary = value as? [String: Any] else { return }
            var  post = Post(user: user, dictionary: dictionary)
            post.id = key
            guard let uid = FIRAuth.auth()?.currentUser?.uid else { return }
            FIRDatabase.database().reference().child("likes").child(key).child(uid).observe(.value, with: { (snapshot) in
                if let value = snapshot.value as? Int, value == 1 {
                    post.hasLiked = true
                } else {
                    post.hasLiked = false
                }
                self.posts.append(post)

                self.posts.sort(by: { (p1, p2) -> Bool in
                    return p1.creationDate.compare(p2.creationDate) == .orderedDescending
                })
                self.collectionView?.reloadData()

            }, withCancel: { (err) in
                print("Failed to fetch info for post")
            })
            print(self.posts)
        })
    }) { (error) in
        print("Failed to fetch posts", error)
    }
  }

1 个答案:

答案 0 :(得分:1)

我不知道Swift,但您可以抓取FIRDatabase.database().reference().child("posts")然后迭代children