I'm coding a viewController which permit to search specifics users. I have a searchBar and a listener:
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
doSearch()
}
func doSearch() {
if let searchText = searchBar.text?.lowercased() {
self.users.removeAll()
self.tableView.reloadData()
Api.User.queryUsers(withText: searchText, completion: { (user) in
self.users.append(user)
self.tableView.reloadData()
})
}
}
The queryUser func:
func queryUsers(withText text: String, completion: @escaping (Userm) -> Void) {
REF_USERS.queryOrdered(byChild: "username_lowercase").queryStarting(atValue: text).queryEnding(atValue: text+"\u{f8ff}").queryLimited(toLast: 5).observeSingleEvent(of: .value) { (snapshot) in
snapshot.children.forEach({ (s) in
let child = s as! DataSnapshot
if let dict = child.value as? [String: Any] {
let user = Userm.transformUser(dict: dict, key: child.key)
completion(user)
}
})
}
}
The problem is that, with Firebase, when I write too fast I can have the same user displayed several times. I would like to have a function which can do something like this:
// if user is not already contained in the array
-> Display it
I tried to use the .contains() function but I have not arrived at a good result, I'm still a beginner in Swift.
Do you have any idea please?