madhatter989
madhatter989

Reputation: 281

load large data from firestore to table view Swift

firestore to store about more than 500 information and I want to display it to table view. Basically, I have successfully display all the data in my cell, but the problem is, it takes more than 1 minute to load all data. While the data loaded, I cannot scroll the table view, unless all data finish load. How to enable scrolling while the data is still loading? If not possible, how to load first 20 data first, and will continue load if user is at the end of the cell? Here is some code that I have tried to get data from firestore:

    func getData () {
    db.collection("fund").getDocuments()
        {
            (querySnapshot, err) in

            if let err = err
            {
                print("Error getting documents: \(err)");
            }
            else
            {
                for document in querySnapshot!.documents {
                    let data = document.data()
                    let agencyPath = data["agensi"] as? String ?? ""
                    let title = data["title"] as? String ?? ""
                    let program = data["program"] as? String ?? ""
                    let perniagaan = data["perniagaan"] as? String ?? ""
                    let newMax = data["max"] as? Int
                    let agencyId = document.documentID
                    let query = Firestore.firestore().collection("Agensi")
                    let newQuery = query.whereField("name", isEqualTo: "\(agencyPath)")

                    newQuery.getDocuments()
                        {
                            (querySnapshot, err) in

                            if let err = err {
                                print("Error getting documents: \(err)");
                            } else
                            {
                                for document in querySnapshot!.documents {
                                    let data = document.data()
                                    let logo = data["logo"] as? String ?? ""
                                    //store to Struct
                                    let newModel = DisplayModel(agency: title, agencyId: agencyId, programTag: program, perniagaanTag: perniagaan, max: newMax, agencyPath: agencyPath, logoUrl: logo, agencyTitle: agencyPath)
                                    self.agencyList.append(newModel)
                                }
                                self.tableView.reloadData()
                                self.dismiss(animated: false, completion: nil)
                            }
                    }
                }

            }
    }
}

display data on cell:

      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cellData: DisplayModel

    if searchController.searchBar.text != "" {
        cellData = filteredData[indexPath.row]

    } else {
        cellData = agencyList[indexPath.row]

    }

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as? HomeTableViewCell

    cell?.agencyName.text = cellData.agency
    cell?.agencyImage.sd_setImage(with: URL(string: "\(cellData.logoUrl ?? "")"), placeholderImage: UIImage(named: "no_pic_image"))

    return cell!


}

Action on last row of cell:

    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {

    if searchController.searchBar.text != "" {
       let lastElement = filteredData.count - 1
        if indexPath.row == lastElement {
            //getData()
            // handle your logic here to get more items, add it to dataSource and reload tableview
        }
    } else {
        let lastElement = agencyList.count - 1
        if indexPath.row == lastElement {
            //getData()
            // handle your logic here to get more items, add it to dataSource and reload tableview
        }

    } 
}

I really have no idea what method I should do to load 20 data first and continue load at the end of cell row, if there is no solution, at least I could scroll the table view during the load session. Thank You, for your information, i just learn swift last month. Thank you for helping me.

Upvotes: 1

Views: 1548

Answers (1)

Andreas Oetjen
Andreas Oetjen

Reputation: 10209

You should definitly adopt the UITableViewDataSourcePrefetching protocol.

Check some blogs, like: https://www.raywenderlich.com/187041/uitableview-infinite-scrolling-tutorial

and adopt it to pagination as described here: https://firebase.google.com/docs/firestore/query-data/query-cursors

Upvotes: 3

Related Questions