-1

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.

amirbt17
  • 533
  • 1
  • 4
  • 13

2 Answers2

0
 You need to use document data property to load MealPlan. Better make it the following way:
    
    1. Define data model as Codable:

import Foundation
import FirebaseFirestore
import FirebaseFirestoreSwift
            
struct MealPlan: Codable {
    @DocumentID var id: String?
    title: String?
}

2. Then use compact map to get mealPlans:

mealPlans = snapshot.documents.compactMap { document -> MealPlan? in
     do {
         let result = try document.data(as: MealPlan.self)
         return result
     }
     catch {
         print(error)
         return nil
     }
}
0

Issue ended up being rooted in my database containing proper case values, whereas my queries were all lowercase. Apparently it's recommended to store my searchable data in its own key, so I created a keywords array for my data to be searched.

My query then became: query = query.whereField("keywords", arrayContains: searchText)

amirbt17
  • 533
  • 1
  • 4
  • 13