I want to search for first and last name from PFUser class using swift and Parse to follow and unfollow users. I used UISearchController, but it does not retrieve the user when I am typing. It just retrieved the users when Im pressing enter. another issue, when I searched for a user, it just retrieved first name not the last name. for example, when I search for Sara John, it retrieved Sara John and Sara March. however, when I typed M, it retrieved Sara March too. The problem is the first and last name together are not retrieved! how can I fix that?
Here is my code, I don't know what Im doing wrong, please help.
func loadSearchUsers(searchString: String) {
let firstName = searchString
let lastName = searchString
let firstNameQuery:PFQuery = PFUser.query()!
firstNameQuery.whereKey("first_name", matchesRegex: "(?i)\(firstName)")
let lastNameQuery:PFQuery = PFUser.query()!
lastNameQuery.whereKey("last_name", matchesRegex: "(?i)\(lastName)")
let query = PFQuery.orQueryWithSubqueries([firstNameQuery, lastNameQuery])
self.searchActive = true
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
//1
if let users = objects {
if (error != nil)
{
let myAlert = UIAlertController(title:"Error", message:error?.localizedDescription, preferredStyle:UIAlertControllerStyle.Alert)
let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
self.searchUsers.removeAll(keepCapacity: false)
self.userIds.removeAll(keepCapacity: false)
self.isFollowing.removeAll(keepCapacity: false)
//2
for object in users {
//3
if let user = object as? PFUser {
//4
if user.objectId! != PFUser.currentUser()?.objectId {
self.searchUsers.append(user)
self.userIds.append(user.objectId!)
self.tableView.reloadData()
let query = PFQuery(className: "followers")
query.whereKey("followFrom", equalTo: PFUser.currentUser()!.objectId!)
query.whereKey("followTo", equalTo: user.objectId!)
query.whereKey("status", equalTo:"followed")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
if objects.count > 0 {
self.isFollowing[user.objectId!] = true
} else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.userIds.count {
self.tableView.reloadData()
self.searchActive = false
}
})
}
}}
}
}
}
// MARK: - Search Bar Delegate Methods
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
let searchString: String = searchBar.text!.lowercaseString
// Force search if user pushes button
if (searchString != "") {
loadSearchUsers(searchString)
}else
{loadSearchUsers("")}
self.userSearchController.resignFirstResponder()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false
searchBar.text = ""
self.resultsController.tableView.reloadData()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = String()
if searchString.lowercaseString.containsString(self.userSearchController.searchBar.text!.lowercaseString) {
if (searchString != "" && !self.searchActive) {
loadSearchUsers(searchString)
}
//Updates the results tableview
self.resultsController.tableView.reloadData()
}
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true
self.resultsController.tableView.reloadData()
}
Use the method below for searching while you are writing simultaneously
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)