正在集合视图中为所有用户加载的帖子Firebase和Swift

时间:2018-11-23 13:02:38

标签: swift firebase firebase-realtime-database

我要做的就是将所有帖子收集到当前用户一定范围内的所有集合视图中。目前,我正在该位置获取所有当前用户的帖子,仅此而已。我不知道如何将其转换为从所有用户那里获取所有帖子。

FetchPostUserIds返回所有用户的快照,UID

geoFire查询仅从当前用户返回postId。我不应该认为

注意:更新代码

var PostKey: String?
var geoFire: GeoFire?
var regionQuery: GFRegionQuery?
var foundQuery: GFCircleQuery?
var geoFireRef: DatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()

    guard let uid = Auth.auth().currentUser?.uid else { return }
    geoFireRef = Database.database().reference().child("posts").child(uid)
    geoFire = GeoFire(firebaseRef: geoFireRef)

}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let userLocation: CLLocation = locations[0] as CLLocation
    let currentUserLocation = userLocation

    let circleQuery = geoFire?.query(at: currentUserLocation, withRadius: 100.0)
    _ = circleQuery?.observe(.keyEntered, with: { (key, location) in
        self.PostKey = key
        self.locationManager.stopUpdatingLocation()
    })
}

 fileprivate func fetchPostsWithUser(user: User) {

    guard let key = PostKey else { return }

    let ref = Database.database().reference().child("posts").child(user.uid).child(key)
    ref.observeSingleEvent(of: .value, with: { (snapshot) in
        self.collectionView?.refreshControl?.endRefreshing()

        guard let dictionary = snapshot.value as? [String: Any] else { return }

        var post = Post(user: user, dictionary: dictionary)
        post.id = key

        self.posts.append(post)
        self.posts.sort(by: { (post1, post2) -> Bool in
            return post1.creationDate.compare(post2.creationDate) == .orderedDescending
        })
        self.collectionView?.reloadData()
    }) { (error) in
        print(error)
    }

}

fileprivate func fetchPostUserIds() {

    let ref = Database.database().reference().child("users")
    ref.observeSingleEvent(of: .value, with: { (snapshot) in

        guard let userIdKey = snapshot.value as? [String: Any] else { return }

        userIdKey.forEach({ (key, value) in
            Database.fetchUserWithUID(uid: key, completion: { (user) in
                self.fetchPostsWithUser(user: user)
            })
        })

    }) { (error) in
        print(error)
    }

}

2 个答案:

答案 0 :(得分:3)

尝试调试并查看函数快照中的内容以及fetchUserWithUID返回的内容

fileprivate func fetchPostUserIds() {

     let ref = Database.database().reference().child("users")
        ref.observeSingleEvent(of: .value, with: { (snapshot) in

            guard let userIdKey = snapshot.value as? [String: Any] else { return }

            userIdKey.forEach({ (key, value) in
            Database.fetchUserWithUID(uid: key, completion: { (user) in
                self.fetchPostsWithUser(user: user)
            })
        })

    }) { (error) in
        print(error)
    }

}

也许有更多信息我可以帮助您

答案 1 :(得分:1)

您将参数user: User传递给方法fetchPostsWithUser,但是您始终使用当前用户

guard let uid = Auth.auth().currentUser?.uid else { return }

也请注意这些

let ref = Database.database().reference().child("users")
ref.observe(.value, with: { (snapshot) in

将加载所有更改,因此请考虑singleObserve