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

1 Answer 1

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()
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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<Array<PFObject>>, Optional<NSError>)') has no member 'count'
findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) first object is an array right? can't you get the count off that? $0 is an array btw
Hi, I've added the code, but when I build and run, as soon as the app is launched, it crashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.