检索所有数据然后过滤到表视图的正确方法是什么?

时间:2019-07-23 05:18:16

标签: swift xcode firebase firebase-realtime-database

我正在成功检索Firebase数据并将其加载到我的表视图中。在初始加载时,我要根据日期查询数据,因此它仅加载日期在当前日期之后的数据。我还有一个“过去”按钮,以相同的方式查询数据,但只显示日期在当前日期之前的数据。它可以正常工作,但是每次单击按钮时我都在检索数据,这似乎会降低性能并有时导致崩溃。这是这样做的方法吗?或者我可以通过什么方式通过按钮加载所有数据和查询/过滤器?

override func viewDidLoad() {
    super.viewDidLoad()
//Retrieving data from Firebase
    refReleases = Database.database().reference().child("releases");
    _ = refReleases.queryOrdered(byChild: "date").queryStarting(atValue: currentDate).observe(.value, with: { (snapshot) in
        if snapshot.childrenCount>0{
            self.releasesData.removeAll()
          for releases in snapshot.children.allObjects as![DataSnapshot]{
                let releaseObject = releases.value as? [String: AnyObject]
                let releaseName = releaseObject?["releaseName"]
                let releaseType = releaseObject?["releaseType"]
                let releasePrice = releaseObject?["releasePrice"]
                let releaseDate = releaseObject?["releaseDate"]
                let releaseImage = releaseObject?["releaseImage"]
                let releasetag = releaseObject?["releasetag"]
                let releaseStyleCode = releaseObject?["releaseStyleCode"]
                let release = ReleaseModel(releasetype: releaseType as! String?, releasename: releaseName as! String?, releaseprice: releasePrice as! String?, releasedate: releaseDate as! String?, releaseimage: releaseImage as! String?, releasetag: releasetag as! String?, releasestylecode: releaseStyleCode as! String?) 
                self.releasesData.insert(release, at: 0)
            }
            self.releasesData.reverse()
            self.tableView.reloadData()
        }

    }
    )}
}

//"Past" button
@IBAction func past(_ sender: UIButton) {
    refReleases = Database.database().reference().child("releases");
    _ = refReleases.queryOrdered(byChild: "date").queryEnding(atValue: currentDate).observe(.value, with: { (snapshot) in
        if snapshot.childrenCount>0{
            self.releasesData.removeAll()
            for releases in snapshot.children.allObjects as![DataSnapshot]{
                let releaseObject = releases.value as? [String: AnyObject]
                let releaseName = releaseObject?["releaseName"]
                let releaseType = releaseObject?["releaseType"]
                let releasePrice = releaseObject?["releasePrice"]
                let releaseDate = releaseObject?["releaseDate"]
                let releaseImage = releaseObject?["releaseImage"]
                let releasetag = releaseObject?["releasetag"]
                let releaseStyleCode = releaseObject?["releaseStyleCode"]
                let release = ReleaseModel(releasetype: releaseType as! String?, releasename: releaseName as! String?, releaseprice: releasePrice as! String?, releasedate: releaseDate as! String?, releaseimage: releaseImage as! String?, releasetag: releasetag as! String?, releasestylecode: releaseStyleCode as! String?)
                self.releasesData.insert(release, at: 0)
            }
            self.tableView.reloadData()
        }

    }
    )}

3 个答案:

答案 0 :(得分:0)

  

但是我每次单击按钮都在检索数据,但速度似乎变慢   性能,有时会崩溃

因为您要阻止主线程,所以可以在Dispatch.main.async {}

中执行此操作

答案 1 :(得分:0)

基本上,每次单击“过去”按钮时,您都会添加观察者。 observe方法将触发每个tiem数据库中的值更改。 另外,删除handleviewDidDisappear

如果要一次手动读取数据,请使用observeSingleEvent方法。 它只会发射一次。

var handle: DatabaseHandle!
override func viewDidLoad() {
    super.viewDidLoad()
//Retrieving data from Firebase
    refReleases = Database.database().reference().child("releases");
    handle = refReleases.queryOrdered(byChild: "date").queryStarting(atValue: currentDate).observe(.value, with: { (snapshot) in
        if snapshot.childrenCount>0{
            self.releasesData.removeAll()

            for releases in snapshot.children.allObjects as![DataSnapshot]{
                let releaseObject = releases.value as? [String: AnyObject]
                let releaseName = releaseObject?["releaseName"]
                let releaseType = releaseObject?["releaseType"]
                let releasePrice = releaseObject?["releasePrice"]
                let releaseDate = releaseObject?["releaseDate"]
                let releaseImage = releaseObject?["releaseImage"]
                let releasetag = releaseObject?["releasetag"]
                let releaseStyleCode = releaseObject?["releaseStyleCode"]



                let release = ReleaseModel(releasetype: releaseType as! String?, releasename: releaseName as! String?, releaseprice: releasePrice as! String?, releasedate: releaseDate as! String?, releaseimage: releaseImage as! String?, releasetag: releasetag as! String?, releasestylecode: releaseStyleCode as! String?) 
                self.releasesData.insert(release, at: 0)
            }
            self.releasesData.reverse()
            self.tableView.reloadData()
        }

    }
    )}
}

override func viewDidDisappear(_ animated: Bool) {
    if let handle = handle {
        refReleases.removeObserver(withHandle: handle)
    }
}

答案 2 :(得分:0)

李开复是对的。您不应每次点击都添加观察者。您可以在过去的按钮中单击检查查询。如果存在,请将其删除。

        private var queryPast: Query?; // be sure firebase query. I do not remember. 
         override func viewDidLoad() 
        {
                super.viewDidLoad()
            //Retrieving data from Firebase
                refReleases = Database.database().reference().child("releases");
                _= refReleases.queryOrdered(byChild: "date").queryStarting(atValue: currentDate).observe(.value, with: { (snapshot) in
                    if snapshot.childrenCount>0{
                        self.releasesData.removeAll()
                      for releases in snapshot.children.allObjects as![DataSnapshot]{
                            let releaseObject = releases.value as? [String: AnyObject]
                            let releaseName = releaseObject?["releaseName"]
                            let releaseType = releaseObject?["releaseType"]
                            let releasePrice = releaseObject?["releasePrice"]
                            let releaseDate = releaseObject?["releaseDate"]
                            let releaseImage = releaseObject?["releaseImage"]
                            let releasetag = releaseObject?["releasetag"]
                            let releaseStyleCode = releaseObject?["releaseStyleCode"]
                            let release = ReleaseModel(releasetype: releaseType as! String?, releasename: releaseName as! String?, releaseprice: releasePrice as! String?, releasedate: releaseDate as! String?, releaseimage: releaseImage as! String?, releasetag: releasetag as! String?, releasestylecode: releaseStyleCode as! String?) 
                            self.releasesData.insert(release, at: 0)
                        }
                        self.releasesData.reverse()
                        self.tableView.reloadData()
                    }

                }
                )}
            }

            //"Past" button
            @IBAction func past(_ sender: UIButton) {
          if (self.queryPast != nil)
          {
             self.queryPast.removeObserver();
              // I DO NOT REMEMBER currently syntax. but you have  do something 
  like that.
          }
                refReleases = Database.database().reference().child("releases");
                self.queryPast = refReleases.queryOrdered(byChild: "date").queryEnding(atValue: currentDate);
       self.queryPast.observe(.value, with: { (snapshot) in
                    if snapshot.childrenCount>0{
                        self.releasesData.removeAll()
                        for releases in snapshot.children.allObjects as![DataSnapshot]{
                            let releaseObject = releases.value as? [String: AnyObject]
                            let releaseName = releaseObject?["releaseName"]
                            let releaseType = releaseObject?["releaseType"]
                            let releasePrice = releaseObject?["releasePrice"]
                            let releaseDate = releaseObject?["releaseDate"]
                            let releaseImage = releaseObject?["releaseImage"]
                            let releasetag = releaseObject?["releasetag"]
                            let releaseStyleCode = releaseObject?["releaseStyleCode"]
                            let release = ReleaseModel(releasetype: releaseType as! String?, releasename: releaseName as! String?, releaseprice: releasePrice as! String?, releasedate: releaseDate as! String?, releaseimage: releaseImage as! String?, releasetag: releasetag as! String?, releasestylecode: releaseStyleCode as! String?)
                            self.releasesData.insert(release, at: 0)
                        }
                        self.tableView.reloadData()
                    }

                }
                )}