How to do a “not in” query in Parse

2019-09-05 04:56发布

I'm building a sort of hot or not style app in Swift where the user can vote: HOT, NOT and MAYBE on an image, respectively.

For every time the user gets to a image, they vote by tapping the IBAction, which triggers a query that shows the result of the total votes and total hots from Parse as shown in my code below.

I plan to have 1,000 images.

Can I preload some of the objectIDs that correspond to each respective image, and then when the user votes on the image, the data is already preloaded/queried from parse? How would I go about that? Someone recommended in my previous question to use a NOT IN query in Parse. How do I do a NOT IN query in Parse and how would I go about doing that?

For now, I'm writing a query for each ObjectID which would take 1000 queries from 1000 different images... Obviously unscalable.

Further code explanations:

The swipePosition variable is just a counter that counts which image the user is on. The images being stored are in an Array for now stored on Xcode. Maybe they can be preloaded as well if they are stored on Parse?

(I am only showing the "hotButtonQuery" function, but there is also a Not and Maybe buttonQuery function.)

Is there a way to simply this code so that it's scalable? Because, as of now, there's no way I can scale past 25 images.

func hotButtonQuery() {
    if swipePosition == 0 {
        var query = PFQuery(className:"UserData")
        query.getObjectInBackgroundWithId("RlvK3GhfqE") {
            (userData: PFObject!, error: NSError!) -> Void in
            if error != nil {

                println(error)
            }

            else {
            userData.incrementKey("totalVotes", byAmount: 1)
            userData.incrementKey("hot", byAmount: 1)

                var updateTotalVotesUILabel = userData.objectForKey("totalVotes") as NSInteger

                var updateHotsUILabel = userData.objectForKey("hot") as NSInteger

                userData.saveInBackground()

                println("parse was updated!")

                self.totalVotesLabel.text = String(updateTotalVotesUILabel)
                self.totalHotsLabel.text = String(updateHotsUILabel)
            }
        }
    } else if swipePosition == 1 {
            var query = PFQuery(className:"UserData")
            query.getObjectInBackgroundWithId("30WlVtgurP") {
                (userData: PFObject!, error: NSError!) -> Void in
                if error != nil {

                    println(error)
                }

                else {
                    userData.incrementKey("totalVotes", byAmount: 1)
                    userData.incrementKey("hot", byAmount: 1)


                    var updateTotalVotesUILabel = userData.objectForKey("totalVotes") as NSInteger

                    var updateHotsUILabel = userData.objectForKey("hot") as NSInteger

                    //println(userData.objectForKey("totalVotes"))
                    //println("total HOTs:")
                    //println(userData.objectForKey("hot"))

                    userData.saveInBackground()

                    println("parse was updated!")


                    self.totalVotesLabel.text = String(updateTotalVotesUILabel)
                    self.totalHotsLabel.text = String(updateHotsUILabel)


                }
            }
    } else if swipePosition == 3 {
        var query = PFQuery(className:"UserData")
        query.getObjectInBackgroundWithId("5D6ARjk3xS") {
            (userData: PFObject!, error: NSError!) -> Void in
            if error != nil {

                println(error)
            }

            else {
                userData.incrementKey("totalVotes", byAmount: 1)
                userData.incrementKey("hot", byAmount: 1)


                var updateTotalVotesUILabel = userData.objectForKey("totalVotes") as NSInteger

                var updateHotsUILabel = userData.objectForKey("hot") as NSInteger

                //println(userData.objectForKey("totalVotes"))
                //println("total HOTs:")
                //println(userData.objectForKey("hot"))

                userData.saveInBackground()

                println("parse was updated!")


                self.totalVotesLabel.text = String(updateTotalVotesUILabel)
                self.totalHotsLabel.text = String(updateHotsUILabel)


            }
        }
    }

2条回答
你好瞎i
2楼-- · 2019-09-05 05:33

An example of Not In:

    var query = PFUser.query()

    if (friendsFilter){
        var friendsRelation:PFRelation = PFUser.currentUser().relationForKey("friendsRelation")
        query = friendsRelation.query()
    }
    else{
        var friendsRelation:PFRelation = PFUser.currentUser().relationForKey("friendsRelation")
        var innerQuery = friendsRelation.query()
        query = PFUser.query()
        query.whereKey("username", doesNotMatchKey: "username", inQuery: innerQuery)
查看更多
地球回转人心会变
3楼-- · 2019-09-05 05:34

just use

query.whereKey("key", doesNotMatchKey: "matchcheck", inQuery: innerQuery)
查看更多
登录 后发表回答