How can I use queryStartingAtValue in my searchcontroller to search for users?

后端 未结 2 2067
有刺的猬
有刺的猬 2021-01-28 20:56

I have previously asked a question about searching for users the most cost-efficient way (without having to load up every user in the entire database.

My code before the

2条回答
  •  我在风中等你
    2021-01-28 21:34

    The code in the question is almost correct, just need to tweak it to ensure the enumeration var is a snapshot and then use it with Mapper.

    As a user enters a user name, recursively call this with each keypress; so if they are typing in Leroy, L is typed first and this code will retrieve all nodes where the username value starts with "L". Then the user types 'e' making it 'Le' etc etc.

    func searchUsers(text: String) {
        if text.count > 0 {
           self.usersArray = [] //clear the array each time
           let endingText = text + "\u{f8ff}"
           databaseRef.child("profile").queryOrdered(byChild: "username")
                                       .queryStarting(atValue: text)
                                       .queryEnding(atValue: endingText)
                                       .observeSingleEvent(of: .value, with: { snapshot in
    
               for child in snapshot.children {
                   let childSnap = child as! DataSnapshot
                   let userObj =  Mapper().map(JSONObject: childSnap.value!)
                   userObj?.uid = childSnap.key
                   if childSnap.key != self.loggedInUser?.uid { //ignore this user
                      self.usersArray.append(userObj!)
                   }
               }
               self.tableView.reloadData()
           })
        }
      } //may need an else statement here to clear the array when there is no text
    

    EDIT:

    OP requested code for handling the search through a searchBar and a if statement to prevent searching if there are is no text. Here's the delegate method for that which calls the searchUsers function above

    func searchBar(_ searchBar: UISearchBar,
                   textDidChange searchText: String) {
    
        self.searchUsers(text: searchText)
    }
    

    EDIT:

    OP wanted to see my viewDidLoad function, as unexciting as it is, so here it is.

    class ViewController: UIViewController, UISearchBarDelegate 
        override func viewDidLoad() {
           super.viewDidLoad()
    
           // Do any additional setup after loading the view.
        }
    

提交回复
热议问题