Я пытаюсь ограничить количество сообщений, которые я получаю, до 4, но по какой-то неизвестной мне причине он возвращает более 4 сообщений без каких-либо данных внутри ячеек.Но если убрать запрос .queryLimited(toLast: 4)
в конце, он работает отлично, но загружает все сообщения.Что я делаю не так?
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation
guard let uid = Auth.auth().currentUser?.uid else { return }
let lat = userLocation.coordinate.latitude
let lon = userLocation.coordinate.longitude
let loc1: CLLocationDegrees = lat
let loc2: CLLocationDegrees = lon
let myLocation: CLLocation = CLLocation(latitude: loc1, longitude: loc2)
let myQuery = geoFire?.query(at: myLocation, withRadius: 100)
var queryHandler = myQuery?.observe(.keyEntered, with: { (key:String!, location:CLLocation!) in
let ref = Database.database().reference().child("posts").child(key).observe(.value, with: { (snapshot) in
let postId = snapshot.key
self.fetchPosts(with: postId)
self.locationManager.stopUpdatingLocation()
})
})
}
fileprivate func fetchPosts(with postId: String) {
self.collectionView?.refreshControl?.endRefreshing()
guard let currentUid = Auth.auth().currentUser?.uid else { return }
//inital data pull
if currentKey == nil {
let ref = Database.database().reference().child("posts").child(postId).queryLimited(toLast: 4)
ref.observe(.value, with: { (snapshot) in
guard let dict = snapshot.value as? [String: Any] else { return }
guard let uid = dict["uid"] as? String else { return }
Database.fetchUserWithUID(uid: uid, completion: { (user) in
guard let dictionary = snapshot.value as? [String: Any] else { return }
var post = Post(postId: snapshot.key, user: user, dictionary: dictionary)
let postId = snapshot.key
post.id = snapshot.key
self.posts.append(post)
self.posts.sort(by: { (post1, post2) -> Bool in
return post1.creationDate.compare(post2.creationDate) == .orderedDescending
})
self.collectionView?.reloadData()
})
})