gpt4 book ai didi

arrays - 在加载时从 Firebase 对数组运行排序?

转载 作者:可可西里 更新时间:2023-11-01 01:30:20 24 4
gpt4 key购买 nike

按下按钮时,我正在对从 Firebase 获取的数组进行某些过滤。例如其中之一是:

self.PersonalSearchesList = PersonalSearchesList.sorted{ $0.users > $1.users }
self.tableView.reloadData()

问题是我想要在 View 最初加载时我想要应用其中一个过滤器..但我不知道在哪里运行过滤器。

如果我在查询后追加数组时运行排序,我的信息/单元格都会被不正确的信息所淹没。

我无法在 viewDiDLoad 中运行它,因为 firebase 的观察者正在 ViewDidLoad 中运行,此时不会填充数组。

编辑:

 currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
if snapshot.value != nil {
if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> {
DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
self.PersonalSearchesList.append(eachSearch)
})
}
}
})
}
}
}
initialLoadOver = true
}

那么什么时候异步结束了呢?我需要在哪一行的右括号之后做这些事情?

最佳答案

您可以为排序列表创建一个实例,当它设置好后,您可以重新加载您的数据

var mySortedList = [YourListObject](){
didSet{
self.tableView.reloadData()
}
}

然后当你的附加操作完成时,只需对其进行排序。

 currentUserFirebaseReference.child("rooms").observeSingleEvent(of: .value) { (snapshot: FIRDataSnapshot) in
if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshots {
DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
if snapshot.value != nil {
if let users = (snapshot.value! as? NSDictionary)?["users"] as? Dictionary<String,AnyObject> {
DataService.ds.REF_USERS.child(topUser).child("pictureURL").observeSingleEvent(of: .value, with: { (snapshot: FIRDataSnapshot) in
self.PersonalSearchesList.append(eachSearch)
})
}
}
})
}
self.mySortedList = PersonalSearchesList.sort({ (element1, element2) -> Bool in
return element1.users! > element2.users!
})
}
}
initialLoadOver = true
}

关于arrays - 在加载时从 Firebase 对数组运行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39524695/

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