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

标签 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)
    }
  }

最佳答案

我不懂 Swift,但你可以获取 FIRDatabase.database().reference().child("posts") 然后遍历 children .

关于ios - 按下按钮时如何添加谷歌地图标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46209386/

相关文章:

swift - 立体 ARSCNview 使 VR 和 AR 混合

ios - Swift中如何在Key和String之间进行转换?

android - 使用 Android/Firebase 实时数据库时,Single.Create()...blockingGet() 在 RxJava 2.1.3 中挂起

angular - 为什么条件 div 不能以 Angular 工作

android - Firebase Android - 更新子值

ios - 一次执行一个调度任务,并在新启动时取消当前

iphone - iOS 中有趣的开关错误

ios - 无法插入新的 socket 连接 : Could not find any information for the class named

ios - QRcode 扫描后我的解雇不起作用怎么办?

ios - 将委托(delegate)设置为现有 View Controller