My searchBar text is updating my query, but not returning the data from Firestore as I would expect.
This is the method in my data model:
func getMealPlans(starred starredOnly:Bool = false, textToQuery searchText:String = "") {
listener?.remove()
let db = Firestore.firestore()
var query:Query = db.collection("mealPlans")
query = query.whereField("title", isGreaterThanOrEqualTo: searchText)
func queryData() {
self.listener = query.addSnapshotListener({ (snapshot, error) in
// Check for errors
if error == nil && snapshot != nil {
var mealPlans = [MealPlan]()
// Parse documents into mealPlans
for doc in snapshot!.documents {
let m = MealPlan(
docID: doc["docID"] as? String,
title: doc["title"] as! String)
mealPlans.append(m)
}
// Call the delegate and pass back the notes in the main thread
DispatchQueue.main.async {
self.delegate?.mealPlansRetrieved(mealPlans: mealPlans)
}
}
})
}
queryData()
}
And this is my search bar method in my view controller:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
let searchedText = searchText.lowercased()
model.getMealPlans(textToQuery: searchedText)
self.tableView.reloadData()
}
I'm wondering if query = query.whereField("title", isGreaterThanOrEqualTo: searchText)
in my model might be the root of my issue. When I print(searchText)
after this code, the searchText
that I type shows up in the console as expected. But the table view doesn't update with the cells that have a matching title
, even if it's an exact match.