【问题标题】:Running sort on an array from Firebase on load?加载时在 Firebase 的数组上运行排序?
【发布时间】:2016-09-16 06:04:00
【问题描述】:

按下按钮时,我正在对从 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
    }

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

【问题讨论】:

  • 显示firebase闭包的代码,你在数组中追加数据。
  • @NiravD 刚刚编辑

标签: arrays swift sorting tableview firebase-realtime-database


【解决方案1】:

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

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
    }

【讨论】:

  • 这是有道理的。我进行了编辑以显示我实际在哪里进行附加。不确定追加操作的确切位置。
  • @Hibernia 在你的 for 循环之后
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-02
  • 2017-07-16
  • 1970-01-01
相关资源
最近更新 更多