我想知道这是否可行以及如何通过 firebase 进行搜索。 我的应用程序有一个文本字段,您可以在其中填写一些通过数组“保存”的规范。
例如:
var arrayOfSpecs = ["13'", "Black"]
现在我想使用 2 个分段选项进行搜索:
搜索规范为 13"或黑色的电子设备之一(此电子设备可能有其他规范;但必须至少包含给定规范之一)
搜索其中一个只有我写的规范的 Elektronics 的名称,规范越少越好,但规范不能超过该人搜索的地方。
示例:搜索“black”“13inch”;搜索结果可能包括具有一个规范的 macbook:“黑色”,但它也可能包括具有 2 个规范的 macbook Pro:“黑色”和“13 英寸”,但它不能包括具有规范的 macbook Air:“黑色”,“13 英寸”、“视网膜”
searchResult 会出现在 tableview 中,或者将 searchResults 的正确名称存储在数组中,我会放在 UITableView 中
在我的数据库下面,我希望你明白我在找什么。
谢谢
最佳答案
将您的 JSON 结构更改为
Electroniks:{
Macbook:{..,
Specification:{
13inch : true,
black : true
},
...
},
iPhone:{..,
Specification:{
13inch : true,
black : true,
camera : true
},
...
},
iPad:{..,
Specification:{
xinch : true,
red : true,
camera : true
},
...
}
}
我不认为你可以一次匹配两个节点查询,但你可以一次匹配一个规范.queryEqualToValue
因此,一种解决方法是:- 检索具有一个特定规范 的所有父节点,然后遍历这些节点以匹配您的其他规范
let parentRef = FIRDatabase.database().reference().child("Elektronics")
parentRef.queryOrderedByChild("Specification/black").queryEqualToValue(true).observeSingleEventOfType(.Value, withBlock: {(snap) in
if let dict = snap.value as? [String:AnyObject]{
for each in dict{
print(each.0) //Would retrieve you every product name with colour black in specification i.e Macbook and iPhone
print((each.1["Specification"] as! NSDictionary).count)//Number of specification
parentRef.child("each.0").child("Specification").child("13inch").observeSingleEventOfType(.Value, withBlock: {(snapshot) in
if snapshot.exists(){
print(each.0) //You found your product's with exactly 13inch and black Specification.This is their name
}
})
}
}else{
print(snap.ref)
}
})
关于ios - 搜索 firebase 数据 : iOS, Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39492535/