Unable to search Firebase query results

Hi, I have a view controller with a searchBar and 2 tableViews. I run a query to Firebase and the results are shown in the top tableView (chargesTableView). The bottom tableView has a listener that when I select a row from the top tableView the values are displayed at the bottom (this is working OK). The searchBar is to filter the results in the top tableView (chargesTableView), the problem I'm having is with the search logic, no matter what I do when I type 1, 2 or 3 letters of the item I'm looking for, I don't get the result I want, sometimes I get an item that contains the first letter or sometimes i don't get anything at all. I don't know what else to do/check/try to get this working as expected. I want to be able to type the first 2,3 letters and get the list of items in the array. Any help is greatly appreciated. Here is my code:


Code Block
class ChargesVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate {
var chargesClassFromCell = [Charges]()
var filteredCharges = [Charges]()
var searchBar = UISearchBar()
var inSearchMode: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
searchBar.delegate = self
}
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchBar.showsCancelButton = true
  }
   
   
  func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchBar.endEditing(true)
    searchBar.showsCancelButton = false
    searchBar.text = nil
    inSearchMode = false
  }
   func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    let searchText = searchBar.text!.lowercased()
    if searchText.isEmpty || searchText == " " {
      inSearchMode = false
      chargesTableView.reloadData()
    } else {
      inSearchMode = true
       
      filteredCharges = chargesClassFromCell.filter({ (searchResult) -> Bool in
        return searchResult.chargeDescription.contains(searchText)
      })
       
      chargesTableView.reloadData()
    }
  }
 func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if tableView == chargesTableView {
      if inSearchMode {
        return filteredCharges.count
      } else {
        return chargesClassFromCell.count
      }
    } else { if tableView == patChargesTableView {
      }
    }
    return patChargesClassFromCell.count
  }
   func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if tableView == chargesTableView {
      let cell = tableView.dequeueReusableCell(withIdentifier: "ChargesCell", for: indexPath) as? ChargesCell
       
      var charges: Charges!
      if inSearchMode {
        charges = filteredCharges[indexPath.row]
      } else {
        cell!.configureChargesListCell(charges: chargesClassFromCell[indexPath.row])
      }
      cell!.charges = charges
       
      return cell!
       
    } else if tableView == patChargesTableView {
      let cell = tableView.dequeueReusableCell(withIdentifier: "PatChargesCell", for: indexPath) as? PatChargesCell
      cell!.configurePatChargesListCell(patCharges: patChargesClassFromCell[indexPath.row])
      cell!.patChargesCount.text = "\(indexPath.row + 1)" + " - "
      return cell!
    }
    return UITableViewCell()
  }


Replies

What is the result that you're currently seeing? Are too many or too few search results being shown?

Please keep in mind that you're filtering for the occurrence of the lowercased search test in chargeDescription, but chargeDescription might contain uppercase letters. It might be beneficial to change the charge's description to lowercase as well.
Code Block swift
searchResult.chargeDescription.lowercased().contains(searchText)


Hi, thank you for your comment. Example, I know I have 2 strings that start with "Inj", when i type that, I don't get those entries and I get 3 records and 1 of them have "inj" but somewhere in the middle. (e.g. Admin vacc pneumonia injection). Is it possible to filter upper and lower case or should I just go with upper case? thank you
Did a test with everything lower case and everything upper case, the results are the same as described above.
I would go with the lowercase only variant. In your filter function, convert the description to lowercase and do the same with the search text. If you just want the descriptions that start with the search string you can use hasPrefix():
Code Block swift
filteredCharges = chargesClassFromCell.filter { searchResult in
return searchResult.chargeDescription.lowercased().hasPrefix(searchText.lowercased())
}


made the changes as suggested and still does not work. Not sure what else to do. Here are 2 screenshots showing the following:
Screenshot 1: 3 items that start with "in"
Screenshot 2: results that only show 2 items instead of 3

dropbox link: https://www.dropbox.com/sh/ow5g7d84gk2ocwo/AADTJhdBxNwgzcl_3hoZTNY2a?dl=0

I have an item that starts with L (lower case) if I type the first letter, something else comes out.
sorry, forgot to add the updated code:

Code Block
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
    searchBar.autocapitalizationType = .none
    let searchText = searchBar.text!.lowercased()
    if searchText.isEmpty || searchText == " " {
      inSearchMode = false
      chargesTableView.reloadData()
    } else {
      inSearchMode = true
       
      filteredCharges = chargesClassFromCell.filter{ searchResult in
        return searchResult.chargeDescription.lowercased().hasPrefix(searchText.lowercased())
      }
       
      chargesTableView.reloadData()
    }
  }