Skip to content

Instantly share code, notes, and snippets.

@zontan
Created September 23, 2019 18:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zontan/d2d0e1bbaec3cc3014d2d628bd215b83 to your computer and use it in GitHub Desktop.
Save zontan/d2d0e1bbaec3cc3014d2d628bd215b83 to your computer and use it in GitHub Desktop.
var resultsArray = [[String:String]]()
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
if let searchText = searchBar.text?.lowercased(), searchText != "" {
resultsArray.removeAll()
queryText(searchText, inField: "username")
queryText(searchText, inField: "email")
} else {
let alert = UIAlertController(title: "Error", message: "Please enter a username.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}
func queryText(_ text: String, inField child: String) {
userRef.queryOrdered(byChild: child)
.queryStarting(atValue: text)
.queryEnding(atValue: text)
.observeSingleEvent(of: .value) { [weak self] (snapshot) in
for case let item as DataSnapshot in snapshot.children {
//Don't show the current user in search results
if self?.currentUser?.uid == item.key {
continue
}
if var itemData = item.value as? [String:String] {
itemData["uid"] = item.key
self?.resultsArray.append(itemData)
}
}
self?.tableView.reloadData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment