I work with Firebase. In ios-application, you must do a search by name through UISearchController.
The data model looks like this:

{ "users": { "Kdkdk2kddjah2ndd": { "name": "Johnyy", "email": "mymail@mail.org", }, } 

I am trying to do a search on the "name" field so that I can search by the first letters entered.
There are two methods that I use for searching:

  let userSearchBarController = UISearchController(searchResultsController: nil) //Массив для найденых пользователей var filteredUsers = [User]() //Метод для UISearchController, срабатывает при активном SearchBar func updateSearchResults(for searchController: UISearchController) { self.filteredUsers.removeAll() print(userSearchBarController.searchBar.text!) filterContentForSearchText(searchText: userSearchBarController.searchBar.text!) } //Метод запроса нужных пользователей func filterContentForSearchText(searchText: String) { if (searchBar != "") { FIRDatabase.database() .reference() .child("users") .queryOrdered(byChild: "name") .queryStarting(atValue: searchText) .observeSingleEvent(of: .value, with: { (snapshot) -> Void in if let userDict = snapshot.value as? [String: AnyObject] { print(snapshot) for key in userDict.keys { if let userData = userDict[key] { //Класс модели User let user = User() let name = userData["name"] let email = userData["email"] user.name = name as? String user.email = email as? String self.filteredUsers.append(user) } } } self.tableView.reloadData() }) } } 

Search does not work as it should. I get all users.
When entering some letters, sometimes I get fewer users, even if there is no such letter anywhere in their fields (userID, name, email), I don’t understand the pattern.

    1 answer 1

    After reading the documentation again, I found a solution. The use of the Unicode character "f8ff" helped.
    Part of the request code itself:

      if (searchText != "") { ref.queryOrdered(byChild: "name") .queryStarting(atValue: searchText) .queryEnding(atValue: "\(searchText)\u{f8ff}") .observeSingleEvent(of: .value, with: { (snapshot) -> Void in })