0

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)


            }
        }
    }
Brian
  • 14,610
  • 7
  • 35
  • 43
Lukesivi
  • 2,206
  • 4
  • 25
  • 43

2 Answers2

1

just use

query.whereKey("key", doesNotMatchKey: "matchcheck", inQuery: innerQuery)
0

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)
ericgu
  • 2,229
  • 23
  • 25
  • So what would that do? – Lukesivi Feb 28 '15 at 19:29
  • The first query gets all users. The inner query gets all Friends related by PFRelation to the current user. The last line translates to Get all Users that I am not friends with. – ericgu Feb 28 '15 at 19:31
  • Great thanks. Any idea how this could this help me with my code? – Lukesivi Feb 28 '15 at 19:36
  • i would suggest breaking down the steps. First query you focus on the total population. Second for the inner query focus on how to obtain those items that you would like to exclude. Make sure both match a unique ID. I apologize if this is vague. I am on the run so this is all I have got for you for now. – ericgu Feb 28 '15 at 19:38