gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:35:56 25 4
gpt4 key购买 nike

我有一个有 2 个按钮的。

  • 当我按下搜索按钮时,应用会转到一个带有我的表格 View Firestore 数据和搜索栏。
  • 当我按下“查询”按钮时,我会根据数组查询我的数据库。

问题是,当我按下“查询”按钮并完成查询时,如果我返回并按下“搜索”按钮,我的应用程序会崩溃并出现以下错误。

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

关于这个问题我搜索了很多,我不明白它是什么意思。如果有人需要代码当然告诉我编辑,但我认为代码不相关。我认为这是更普遍的错误。

编辑

一些来 self 批量使用的搜索按钮的代码。

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")
}
}
}

}

最佳答案

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

let settings = FirestoreSettings()
settings.isPersistenceEnabled = false

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

这应该只在应用程序的整个运行过程中执行一次,而您似乎要执行多次。

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

关于swift - 尝试在 Firestore 上进行另一个查询时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com