2017-09-14 62 views
0

是否可以从firebase db中的父级获取所有子节点?按下按钮时,如何添加Google地图标记?

想说的是: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

我不知道斯威夫特,但你可以获取FIRDatabase.database().reference().child("posts"),然后遍历children

+0

是的,它会迭代用户ID。但不是用户ID里面的帖子 –

+0

在子快照中使用'children' –

相关问题