gpt4 book ai didi

swift - Firebase 和 Swift 限制帖子编号错误

转载 作者:行者123 更新时间:2023-11-28 13:50:40 25 4
gpt4 key购买 nike

我试图将我获取的帖子数量限制为 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()
})
})

最佳答案

我觉得行

let ref = Database.database().reference().child("posts").child(postId).queryLimited(toLast: 4)

在 postId 下取最后 4 个 child 。

如果您只想要 4 个帖子,您可以尝试将 .queryLimited(toLast: 4) 移动到您首先查询“帖子”的此引用的末尾:

let ref = Database.database().reference().child("posts").child(key)

关于swift - Firebase 和 Swift 限制帖子编号错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54660894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com