使用Swift 4从Firebase提取postId的帖子

时间:2018-11-20 00:38:12

标签: swift firebase firebase-realtime-database geofire

我要做的就是将半径范围内的帖子添加到集合视图中。当前,当用户输入半径时,我得到了所有postId,但是没有任何东西被附加到集合视图中,我不知道为什么。非常感谢您的帮助。

var posts = [Post]()
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

        guard let user = self.user else { return }
        let ref = Database.database().reference().child("posts").child(key)
        ref.observeSingleEvent(of: .value, with: { (snapshot) in

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

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

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

        }, withCancel: { (error) in
            print("There was an error getting the posts:", error)
        })

    })
}

    fileprivate func fetchPostUserIds() {
        Database.database().reference().child("users").observe(.value, with: { (snapshot) in

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

                let uidKey = key
                self.geoFireRef = Database.database().reference().child("posts").child(uidKey)
                self.geoFire = GeoFire(firebaseRef: self.geoFireRef)

            })
        }) { (err) in
            print("Failed to get user Id", err)
        }
    }

Firebase Database

0 个答案:

没有答案