I wanted to know if it's possible and how you can search through firebase. My app has a textfield where you can fill in some specifications that's get 'saved' in through an array.
for example:
var arrayOfSpecs = ["13'", "Black"]
Now I want to search with 2 segmented options:
Search for the name of one of the Elektronics that has a spec of 13" or is black" (this elektronic device may have other specs; but has to include at least one of the given specs)
Search for the name of one of the Elektronics that only has the specs I wrote, less specs are ok but no more specs than where the person searched for.
Example: Search for "black" "13inch"; the searchResults could include a MacBook with one spec: "black", but it could also include a MacBook Pro with 2 specs: "black" and "13inch", but it can not include a MacBook Air with specs: "Black", "13 inch", "Retina"
The searchResult would come in a tableview, or would store the correct names of the searchResults in array, which I would put in a UITableView
Below my database, I'll hope you understand what I'm looking for.
thanks
Change your JSON Structure to
Electroniks:{
Macbook:{..,
Specification:{
13inch : true,
black : true
},
...
},
iPhone:{..,
Specification:{
13inch : true,
black : true,
camera : true
},
...
},
iPad:{..,
Specification:{
xinch : true,
red : true,
camera : true
},
...
}
}
I don't think you can match two node queries at once , But what you can is, you can match one specification .queryEqualToValue
at a time
So a workaround would be :- retrieve all the parentNodes which have one particular Specification and then iterate through those to match your other Specification
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)
}
})