Arrays 在Firebase加载的阵列上运行排序?

Arrays 在Firebase加载的阵列上运行排序?,arrays,swift,sorting,tableview,firebase-realtime-database,Arrays,Swift,Sorting,Tableview,Firebase Realtime Database,按下按钮时,我正在对从Firebase获得的阵列进行某些筛选。例如,其中之一是: self.PersonalSearchesList = PersonalSearchesList.sorted{ $0.users > $1.users } self.tableView.reloadData() 问题是,我想在视图最初加载时应用一个过滤器,但我不知道在哪里运行过滤器 如果我在查询后追加数组时运行排序,我的信息/单元格会被错误信息填满 我无法在viewDiDLoad中运行它,

按下按钮时,我正在对从Firebase获得的阵列进行某些筛选。例如,其中之一是:

self.PersonalSearchesList = PersonalSearchesList.sorted{ $0.users > $1.users }
        self.tableView.reloadData()
问题是,我想在视图最初加载时应用一个过滤器,但我不知道在哪里运行过滤器

如果我在查询后追加数组时运行排序,我的信息/单元格会被错误信息填满

我无法在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
    }
currentUserFirebaseReference.child(“房间”).observeSingleEvent(of:.值){(快照:FIRDataSnapshot)位于
如果让快照=snapshot.children.allObjects为?[FIRDataSnapshot]{
用于管理单元快照{
DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of:.值,其中:{(快照:FIRDataSnapshot))位于
如果snapshot.value!=nil{
如果让用户=(snapshot.value!as?NSDictionary)?[“用户”]as?Dictionary{
DataService.ds.REF_USERS.child(topUser.child(“pictureURL”).observeSingleEvent(of:.值,其中:{(快照:FIRDataSnapshot))位于
self.PersonalSearchesList.append(每个搜索)
})
}
}
})
}
}
}
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
    }
currentUserFirebaseReference.child(“房间”).observeSingleEvent(of:.值){(快照:FIRDataSnapshot)位于
如果让快照=snapshot.children.allObjects为?[FIRDataSnapshot]{
用于管理单元快照{
DataService.ds.REF_INTERESTS.child(interestKey).observeSingleEvent(of:.值,其中:{(快照:FIRDataSnapshot))位于
如果snapshot.value!=nil{
如果让用户=(snapshot.value!as?NSDictionary)?[“用户”]as?Dictionary{
DataService.ds.REF_USERS.child(topUser.child(“pictureURL”).observeSingleEvent(of:.值,其中:{(快照:FIRDataSnapshot))位于
self.PersonalSearchesList.append(每个搜索)
})
}
}
})
}
self.mySortedList=PersonalSearchesList.sort({(element1,element2)->Bool-in
返回element1.users!>element2.users!
})
}
}
initialLoadOver=true
}

显示firebase闭包的代码,在该代码中,您正在数组中添加数据。@NiravD刚刚编辑过,这很有意义。我编辑以显示我实际在哪里进行追加。不确定追加操作在何处完成。@在for循环之后是Hibernia