尝试在Firestore上进行另一个查询时出错

时间:2019-06-17 13:54:15

标签: swift firebase google-cloud-firestore

我有一个具有2个按钮的按钮。

  • 当我按下搜索按钮时,该应用会与我的应用一起进入表格视图 Firestore数据和searchBar。
  • 当我按下“查询”按钮时,我将基于Array查询数据库。

问题是,当我按下查询按钮并完成查询时,如果我返回并按下搜索按钮,则我的应用程序将崩溃,并显示以下错误。

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'FIRESTORE INTERNAL ASSERTION FAILED: Firestore instance has already been started and its settings can no longer be changed. You can only set settings before calling any other methods on a Firestore instance.'
*** First throw call stack:
(0x1d95ed27c 0x1d87c79f8 0x1d9506988 0x1da016ee8 0x1050f2f7c 0x104f0aaa4 0x104f1be30 0x104e0cba4 0x104e0a6a8 0x104e0ab78 0x205949fc8 0x20594a3cc 0x2058a74c8 0x2058bb4b8 0x2058bc8e0 0x20589faf0 0x20638aed0 0x1ddaa1a20 0x1ddaa69c8 0x1dda092d0 0x1dda37330 0x1dda37f20 0x1d957e5f8 0x1d9579320 0x1d957989c 0x1d95790b0 0x1db77979c 0x205ef3978 0x104e22e58 0x1d903e8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException

我搜索了很多有关此问题的内容,但我不明白这意味着什么。当然,如果有人需要代码,请告诉我进行编辑,但是我认为代码无关。我认为这是更普遍的错误。

编辑

我使用批量搜索按钮中的一些代码。

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: $0.data())})
                    self.ingredientsArray.append(contentsOf: res)

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

                self.lastDocument = querySnapshot!.documents.last
            }
        }
    }

    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: $0.data())})
                    self.ingredientsArray = self.searchedIngredientsArray
                    self.tableView.reloadData()
                    self.fetchMoreIngredients = true

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

    }

1 个答案:

答案 0 :(得分:1)

您正在使用以下代码为Firestore启用本地缓存:

$scope.dtOptions = DTOptionsBuilder.newOptions()
    .withOption('ajax', function() {            
        MyService.getAllPerson($scope.age).then(function(reslt) {                   
            $scope.listOfPersonByage=reslt;
        });             
    })
    .withOption('processing', true) //for show progress bar
    .withOption('serverSide', true) // for server side processing
    .withDataProp('data')
    //...

});

对于整个应用程序运行,此操作应该只执行一次,而您似乎要多次执行。

通常的解决方案是将这段代码移到您的AppDelegate中,以便它遵循应用程序的生命周期。但是,由于默认情况下,iOS上的Firestore启用了本地持久性,因此您也可以简单地删除此代码并获得相同的行为。