如何在 tableView 中加载搜索数据时停止 Firestore 分页

How to stop Firestore pagination when searched data are loaded in tableView

我使用无限滚动来填充我的 Firestore 数据 tableView.Also 我有一个用于搜索的搜索栏。 我成功地对我的数据进行分页,但是当我搜索某些内容并使用已找到的数据重新加载 tableView 时,分页在我滚动后再次开始。无法禁用滚动,因为数据可能超过屏幕高度。

下面我提供我的代码。

var fetchMoreIngredients = false
var reachEnd = false
let leadingScreensForBatching: CGFloat = 10.0
//When user scrolls down it begins to fetch more.
     func scrollViewDidScroll(_ scrollView: UIScrollView) {

        let off = scrollView.contentOffset.y
        let off1 = scrollView.contentSize.height

        if off > off1 - scrollView.frame.height * leadingScreensForBatching{
            if !fetchMoreIngredients && !reachEnd{
                print(fetchMoreIngredients)
                beginBatchFetch()
            }
        }
    }
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        guard !searchText.isEmpty else {
            ingredientsArray.removeAll()
            beginBatchFetch()
            return
        }

    }

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
        let text = searchBar.text!.lowercased()
            searchIngredients(text: text)
            self.searchBarIngredient.endEditing(true)
            print("\(searchIngredients(text: text))")

    }
func  beginBatchFetch() {

        let settings = FirestoreSettings()
        settings.isPersistenceEnabled = false

        fetchMoreIngredients = true
        let db = Firestore.firestore()
        db.settings = settings

        var query: Query!

        if ingredientsArray.isEmpty {
            SVProgressHUD.show()

            query = db.collection("Ingredients").limit(to: 4)
            print("First 4 ingredient loaded")
        } else {
            query = db.collection("Ingredients").start(afterDocument: lastDocument).limit(to: 4)
            print("Next 4 ingredient loaded")
        }

        query.getDocuments { (querySnapshot, err) in
            if let err = err {
                print("\(err.localizedDescription)")
                print("Test Error")
            } else if querySnapshot!.isEmpty {
                self.fetchMoreIngredients = false
                return
            } else {
                if (querySnapshot!.isEmpty == false){
                    let res = querySnapshot!.documents.compactMap({Ingredients(dictionary: [=13=].data())})
                    self.ingredientsArray.append(contentsOf: res)

                    self.tableView.reloadData()
                    self.fetchMoreIngredients = false
                    SVProgressHUD.dismiss()
                }

                self.lastDocument = querySnapshot!.documents.last
            }
        }
    }
    func searchIngredients(text: String){

        fetchMoreIngredients = false

        let db = Firestore.firestore()

        db.collection("Ingredients").whereField("compName", arrayContains: text).getDocuments{ (querySnapshot, err) in
            if let err = err {
                print("\(err.localizedDescription)")
                print("Test Error")
            } else {
                if (querySnapshot!.isEmpty == false){
                    self.searchedIngredientsArray = querySnapshot!.documents.compactMap({Ingredients(dictionary: [=14=].data())})
                    self.ingredientsArray = self.searchedIngredientsArray
                    self.tableView.reloadData()


                }else{
                    print("No Ingredients were found")
                }
            }
        }

    }

提前致谢!

解决方案

我终于找到了解决办法。在 searchIngredients(text: String) 函数中,我应该在开头和闭包内声明 fetchMoreIngredients 为真。

func searchIngredients(text: String){

    fetchMoreIngredients = true

    let db = Firestore.firestore()

    db.collection("Ingredients").whereField("compName", arrayContains: text).getDocuments{ (querySnapshot, err) in
        if let err = err {
            print("\(err.localizedDescription)")
            print("Test Error")
        } else {
            if (querySnapshot!.isEmpty == false){
                self.searchedIngredientsArray = querySnapshot!.documents.compactMap({Ingredients(dictionary: [=10=].data())})
                self.ingredientsArray = self.searchedIngredientsArray
                self.tableView.reloadData()
                self.fetchMoreIngredients = true


            }else{
                print("No Ingredients were found")
            }
        }
    }

}