简体   繁体   English

Firebase数据需要很长时间才能加载到TableView Swift 3中

[英]Firebase data taking ages to load in tableview swift 3

I have a tableview that loads data from Firebase, and on load it seems to take ages, around 10-15 seconds before any data is shown in the tableview. 我有一个可从Firebase加载数据的表视图,加载时似乎要花一些时间,大约需要10到15秒才能在该表视图中显示任何数据。 it also appears thet the app is frozen while this data is loading. 加载该数据时,该应用也被冻结。

my function for getting the data is: this is called in viewDidLoad 我获取数据的功能是:在viewDidLoad中调用

  func getTrackData() {
    let result = FIRDatabase.database().reference(withPath: "tracks")
    result.observe(.value, with: { snapshot in
        var newItems: [newTracks] = []
        for item in snapshot.children {
            let trackDetails = newTracks(snapshot: item as! FIRDataSnapshot)
            newItems.append(trackDetails)
        }
        self.items = newItems
        self.items.sort(by: {$0.distance < $1.distance})
        self.tableView.reloadData()
    })

}

and my tableview is as follows: 和我的tableview如下:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if let cell = tableView.dequeueReusableCell(withIdentifier: "TrackCell", for: indexPath) as? TrackCell {

        let tr: newTracks!
        if inSearchMode {
            tr = filteredTrack[indexPath.row]
            cell.configureCell(track: tr)
        } else {
            tr = items[indexPath.row]
            cell.configureCell(track: tr)
        }
        cell.configureCell(track: tr)

        cell.completion = {
            let coordinate = CLLocationCoordinate2DMake(tr.lat,tr.lon)
            let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate,addressDictionary:nil))
            mapItem.name = tr.name
            mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey :MKLaunchOptionsDirectionsModeDriving])
            return()
        }

        cell.completion1 = {
            let url = URL(string: tr.link)!
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.open(url)
            }
            return()
        }
        return cell
    } else {
        return UITableViewCell()
    }
}

My question is, is this code written ok, or is there a better way to do it. 我的问题是,此代码是否可以编写,还是有更好的方法来实现? I have a concern that my function is getting each item and then reloading the tableview, and this may be slowing things down significantly. 我担心我的功能是获取每个项目,然后重新加载tableview,这可能会大大降低速度。 My data isnt massive, here is an example. 我的数据不是很大,这是一个例子。

在此处输入图片说明

that shows 1 full record, there are 131 of those records in total. 显示1条完整记录,总共131条记录。

Figured this out now, I had a calculation for distance in miles from current location being worked out with each set of data being loaded. 现在解决了这个问题,我计算出要加载的每组数据与当前位置之间的距离(以英里为单位)。 This significantly slowed down the tableview population. 这大大降低了Tableview人口。

Just need to figure how to do this calculation now, but maybe after the data has loaded 现在只需要弄清楚如何执行此计算,但是也许在数据加载之后

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM