2017-09-14 92 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' –

相關問題