gpt4 book ai didi

ios - Firebase queryOrderedByKey 不按时间顺序对帖子进行排序

转载 作者:行者123 更新时间:2023-11-28 15:46:56 25 4
gpt4 key购买 nike

我的应用程序有一个 Collection View ,它显示来自 Firebase 的照片/图像行,我想让它们按照添加的顺序加载,最新的帖子在顶部。我认为默认情况下使用 queryOrderedByKey 可以做到这一点,这就是我在 fetch 函数中使用的,但是帖子乱序了。

这就是我目前获取帖子的方式:

func fetchPosts() {

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

let users = snapshot.value as! [String : AnyObject]

for (_, value) in users {

if let uid = value["uid"] as? String {

if uid == FIRAuth.auth()?.currentUser?.uid {

if let followingUsers = value["following"] as? [String : String] {

for (_, user) in followingUsers {
self.following.append(user)
}
}
self.following.append(FIRAuth.auth()!.currentUser!.uid)

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let postsSnap = snap.value as! [String : AnyObject]

for (_, post) in postsSnap {
if let userID = post["userID"] as? String {
for each in self.following {
if each == userID {

let posst = Post()
if let poster = post["poster"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {

posst.poster = poster
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
if let people = post["peopleWhoLike"] as? [String : AnyObject] {
for (_, person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
posts.append(posst)
}
}
}
self.collectionView.reloadData()
}
}
})
ref.removeAllObservers()
}
}
}
})
}

如何让帖子按最新排序?

编辑 2:更新 - 现在从最旧 -> 最新排序

func fetchPosts() {

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

let users = snapshot.value as! [String : AnyObject]

for (_, value) in users {

if let uid = value["uid"] as? String {

if uid == FIRAuth.auth()?.currentUser?.uid {

if let followingUsers = value["following"] as? [String : String] {

for (_, user) in followingUsers {
self.following.append(user)
}
}
self.following.append(FIRAuth.auth()!.currentUser!.uid)

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in

for postSnapshot in snap.children.allObjects as! [FIRDataSnapshot] {
let value = postSnapshot.value as! [String : AnyObject]

if let userID = value["userID"] as? String {
for each in self.following {
if each == userID {

let posst = Post()
if let poster = value["poster"] as? String, let likes = value["likes"] as? Int, let pathToImage = value["pathToImage"] as? String, let postID = value["postID"] as? String {

posst.poster = poster
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
if let people = value["peopleWhoLike"] as? [String : AnyObject] {
for (_, person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
posts.append(posst)
}
}
}
self.collectionView.reloadData()
}
}
})
ref.removeAllObservers()
}
}
}
})
}

最佳答案

查询按请求的顺序返回匹配子节点的快照。这意味着结果由三部分组成:

  1. 项目键
  2. 项目值(value)
  3. 项目相对于彼此的顺序

但是您对快照所做的第一件事是:

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let postsSnap = snap.value as! [String : AnyObject]

您将快照转换为字典。字典只有两个空间:键和值。所以此时顺序丢失了,因为字典有一个未定义的顺序。

按照您请求的顺序访问结果的正确方法是使用快照的内置子集合来迭代它们:

ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snapshot) in

for postSnapshot in snapshot.children {
let value = postSnapshot.value as! [String : AnyObject]

这将按照您查询的顺序遍历匹配的子项。

关于ios - Firebase queryOrderedByKey 不按时间顺序对帖子进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42887272/

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