gpt4 book ai didi

Swift:在 Firebase 数据库中搜索特定值并查找所有关联数据

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

enter image description here enter image description here

我有数据上传到我的数据库。我正在尝试实现搜索功能,我可以通过名称搜索某些内容,如果找到该名称,则使用与该名称对应的数据自动填充文本字段。例如,如果我搜索“Pepsi Max”,我想在我的数据库中找到 pepsi max,然后显示价格/位置/评级等。

我目前有一个搜索功能,但它只搜索整个数据库并打印所有值。

func searchT() {

let pub = pubName.text

print(pub)


let databaseRef = Database.database().reference().child("Drinks")
let query = databaseRef.queryOrdered(byChild: "pub").queryStarting(atValue: pub).queryEnding(atValue: "\(String(describing: pub))\\uf8ff")

query.observeSingleEvent(of: .value) { (snapshot) in
guard snapshot.exists() != false else { return }
print(snapshot.value as Any)
DispatchQueue.main.async {

guard let dict = snapshot.value as? [String:Any] else {
print(snapshot)
return
}

let pubName = dict["pub"] as? String
let pubLocation = dict["location"] as? String
let price = dict["price"] as? String
let rating = dict["rating"] as? String
let comment = dict["comment"] as? String

self.pubName.text?.append(pubName!)
self.pubLocation.text?.append(pubLocation!)
self.price.text?.append(price!)
self.rating.text?.append(rating!)
self.comment.text?.append(comment!)

}
}
}

您会注意到,在这个函数中,我正在搜索数据“pubName”(我认为我在第一行中设置不正确,但不确定如何更正它)。此函数在将 textViews 设置为值的第一行崩溃,因为存在“展开可选值时为零”

如何通过 pubName 搜索,找到相应的值,然后将文本字段设置为数据库中与搜索值相关的剩余数据。

提前致谢,E

最佳答案

<强>1。实时数据库

由于您没有包含数据库的结构,我假设您的饮料数据库结构如下:

Screenshot of my Realtime database for this answer

{
"Drinks" : {
"-LYiUHm4vtrB3LqCBxEc" : {
"location" : "toronto",
"name" : "pepsi max",
"price" : 13.5,
"rating" : 3.6
},
"-LYiUHm5Lgt3-LENTdBZ" : {
"location" : "new york",
"name" : "diet coke",
"price" : 15.45,
"rating" : 5
},
"-LYiUHm5Lgt3-LENTdB_" : {
"location" : "chicago",
"name" : "mountain dew",
"price" : 2,
"rating" : 2
},
"-LYiUHm5Lgt3-LENTdBa" : {
"location" : "vancouver",
"name" : "sprite",
"price" : 6.98,
"rating" : 4.5
}
}
}

<强>2。 swift 4.0

现在,要按名称搜索任何饮料,请使用以下代码:

func search(drinkName: String) {
let databaseRef = Database.database().reference().child("Drinks")
let query = databaseRef.queryOrdered(byChild: "name").queryStarting(atValue: drinkName).queryEnding(atValue: "\(drinkName)\\uf8ff")

query.observeSingleEvent(of: .value) { (snapshot) in
guard snapshot.exists() != false else { return }
//print(snapshot.value)
DispatchQueue.main.async {
// Update TextFields here
}
}
}

The \uf8ff character used in the query above is a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with a b.

来源:https://firebase.google.com/docs/database/rest/retrieve-data

Note: queryOrderedByChild() is case-sensitive. It is nice practice to save all fields lowercased in database as this makes it easier to query data. You can always format strings in front end.

<强>3。将“.indexOn”添加到实时数据库的规则中

为了上述查询工作并获得更好的性能,您需要在要搜索的字段上设置索引。您可以通过转到“规则”选项卡并添加如下索引来执行此操作:

{
"rules": {
".read": true,
".write": true,
"Drinks": {
".indexOn": "name"
}
}
}

来源:More information on indexing data

更新问题的更新答案:

func searchT() {
// You must cast pub variable as String.
guard let pub: String = pubName.text else { return }

print(pub)


let databaseRef = Database.database().reference().child("Drinks")

let query = databaseRef.queryOrdered(byChild: "pub").queryStarting(atValue: pub).queryEnding(atValue: "\(String(describing: pub))\\uf8ff")

query.observeSingleEvent(of: .value) { (snapshot) in
guard snapshot.exists() != false else {
print("failing here")
return }
print(snapshot.value as Any)
DispatchQueue.main.async {

guard let dict = snapshot.value as? [String:Any] else {
print(snapshot)
return
}

let pubName = dict["pub"] as? String
let pubLocation = dict["location"] as? String
let price = dict["price"] as? String
let rating = dict["rating"] as? String
let comment = dict["comment"] as? String
}
}
}

关于Swift:在 Firebase 数据库中搜索特定值并查找所有关联数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54695618/

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