0

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

Nick89
  • 2,948
  • 10
  • 31
  • 50

1 Answers1

1

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()
  }
}
David Yang Liu
  • 1,170
  • 2
  • 10
  • 19
  • I did wonder if that's how i'd do it. I have run that code but i get this: `Value of tuple type '([PFObject]?, NSError?)' (aka '(Optional>, Optional)') has no member 'count'` – Nick89 Feb 10 '16 at 22:33
  • findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) first object is an array right? can't you get the count off that? $0 is an array btw – David Yang Liu Feb 10 '16 at 22:50
  • Hi, I've added the code, but when I build and run, as soon as the app is launched, it crashes. – Nick89 Feb 11 '16 at 22:37