How to check if UISearchBar has returned results using query.whereKey in Swift

319 views Asked by At

I have a PFQueryTableViewController, and I have added a UISearchBar to it. In my QueryForTable function, I run this code to check if the text entered into the searchBar matches any value in my Parse column, and if so it shows only those films in the table:

override func queryForTable() -> PFQuery {
let query = PFQuery(className: "Reviews")
query.orderByDescending("createdAt")

if filmSearchBar.text != "" {

    query.whereKey("FilmName", containsString: filmSearchBar.text!)

}

    return query

}

This currently all works fine with no issues.

What I would like to do, is if no results are found, display an Alert to the user to let them know nothing was found. Currently, if the user searches for something random, that isn't in my Parse database, it just shows a blank table (which is technically correct) - but i'd like to know how I can do a check first, so if the search entry doesn't match anything, show THIS alert, else if it does, then it just shows the films like it currently does.

Any help appreciated, thanks

1

There are 1 answers

3
David Yang Liu On

can't you just count the objects with findObjects or findObjectsInBackgroundWithBlock

if filmSearchBar.text != "" {
    query.whereKey("FilmName", containsString: filmSearchBar.text!)
}
query.findObjectsInBackgroundWithBlock{ (array:[PFObject]?, error:NSError?) in 
   if array?.count == 0 {
    showAlert()
  }
}