Reputation: 41
I'm trying to apply search in my app but it shows a random product not the same of what I'm typing at searchBar
method :
static func getSearch ( name : String ,completion : @escaping (_ product : ProductObject) -> ()) {
let path = Firestore.firestore().collection("Products").whereField("name" , isLessThanOrEqualTo: name)
path.addSnapshotListener { (query, error) in
if error != nil {return}
guard let doucments = query?.documents else {return}
for doc in doucments {
if let data = doc.data() as [String: AnyObject]? {
let newData = ProductObject(dictionary: data)
completion (newData)
}
}
}
}
at searchBar text did change :
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.products = []
ProductApi.getSearch(name: searchText) { (pro) in
self.products.append(pro)
DispatchQueue.main.async {
self.collectionView.reloadData()
}
}
if searchTxt.text?.count == 0 {
DispatchQueue.main.async { searchBar.resignFirstResponder() }
}
collectionView.reloadData()
}
Upvotes: 1
Views: 642
Reputation: 41
this was the best way I found :
let path = Firestore.firestore().collection("Products").order(by: "name").start(at: [name]).end(at: ["(name) \u{f8ff}"])
Upvotes: 3