1

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 and then for each respective vote the IBAction 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 all 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?

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

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...

Thanks a lot!

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)


            }
        }
    }
Lukesivi
  • 2,206
  • 4
  • 25
  • 43

1 Answers1

0

Respectfully, I would suggest rethinking your whole approach. Don't store objectIds in your code. Don't repeat essentially the same block of code over and over. Keep it simple:

  1. Retrieve and display image
  2. Capture vote and save object
  3. Query for next object and repeat process

If you want to reduce the number of queries, grab them in batches of 10 or so. No sense loading everything since the user is not likely to click through all 1000.

The only thing left to figure out is how you will be keeping track of which images the user has already voted on. You could do this client-side or within Parse. There are a few decent approaches.

picciano
  • 22,341
  • 9
  • 69
  • 82
  • Thanks for the honesty. I absolutely agree with your approach. I am new swift and programming, do you have any suggestions of what I can do? How I can structure it? that would help immensely... Thanks – Lukesivi Feb 27 '15 at 23:00
  • On a recent project (I've Got A Hunch) for iOS, I used a `Response` table in Parse to track the users' actions. So the query to find the next question included a where clause to exclude question that the user has already responded to. Since that's a different question, I would encourage you to ask a new question about how to do a "not in" query in Parse. – picciano Feb 28 '15 at 02:17