Search code examples
iosswiftparse-platformpfquery

How to only display items in my PFQueryTableViewController if set to true in Parse - Swift


So as an overview, I have a Parse backend all set up, which populates my PFQueryTableViewController. My table in Parse contains films, with information about them.

Everything works lovely.

What I'd like to do:

Currently, my app will display all the items I have in my database. Is it possible to only show the items, if say there is a boolean which is set to true for that film?

For example:

enter image description here Here is a screenshot of my Parse database. If I added another column to the end of type, Boolean, called "toDisplayInApp" and then set say 2 of the films to True - can it only populate my table in the app with those 2 films that have been set to True? (and the others set to False obviously)

I imagine this is possible, but not really sure on where/how to add this. Is it to do with using whereKey in Parse? Below is my code where I make my initial Query, do I add it in there? And if so, what would I add?

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

return query

}

Overall goal

I'd like to be able to create the films in my database, and turn them on in the app when I'm ready.

Thanks in advance and let me know if you need any more information.


Solution

  • Yes, it is possible to only get objects where a Boolean is set to true. To do this, you can call

    query.whereKey("toDisplayInApp", equalTo:true)
    

    Your full code will be:

    override func queryForTable() -> PFQuery {
        let query = PFQuery(className: "Reviews")
        query.orderByDescending("createdAt")
        query.whereKey("toDisplayInApp", equalTo:true)
    
        return query
    }
    

    You can learn more about queries and what you can do with them at: iOS Queries - Parse.com