I'm building a rating app in Swift where I'm storing an image's average votes, total votes, and current vote in Parse. I want to update the row without having the objectId.
Giving each image an objectId + querying Parse for every time that image gets voted on seems a bit too much considering I have about 1000 images.
var userData = PFObject(className:"UserData")
userData.setObject(0, forKey: "imageNumber")
userData.setObject(0, forKey: "totalVotes")
userData.setObject(0, forKey: "Average")
userData.saveInBackground()
For every time the user taps to vote on an Image (1-10) by tapping on an IBAction I want to update Parse by incrementing 1) the total # of votes, 2) the new average from this newest vote and 3) i want to update the user interface to show this.
For now, this is what I have attempted with no luck. This is buggy and doesn't work, and I'm basing it off of other questions posted as well as from the Parse docs.
This is what I have for now:
@IBAction func one(sender: UIButton) {
//This is "one" vote. There are 9 more IBActions that do the exact same thing
//Here is where I would update the labels on the UI
// I would also have a function that is triggered to update parse based on this increment to the total # of votes and the average.
// This would also update the UI based on the votes received, querying the data from Parse.
}
func updateParse() {
var randNumber = Int32()
counter = 0
counter++
var query = PFQuery(className:"UserData")
query.countObjectsInBackgroundWithBlock {
(count: Int, error: NSError!) -> Void in
if error == nil {
randNumber = arc4random_uniform(count)
query2.whereKey("ImageNumber", equalTo:randNumber)
query2.getFirstObjectInBackgroundWithBlock {
(userData: PFObject!, error: NSError!) -> Void in
if error != nil {
println(error)
} else {
let votes = userData["totalVotes"] as Int
}
}
}
}
}
I'm attempting to find a number that matches the image number so that based on what image the user is on, when the user taps the vote, the IBaction will trigger the function that will look for which objectID the image matches with Parse.
I'm having trouble querying Parse so that it updates the image that it's on, because I don't know how to update JUST the row.
Any ideas? Thank you so much...
PS,
To make things a little more clear thanks to feedback from questions:
I don't need 100000 objectIds. I just need to update the respective row for when the user is on the specific image.
For example: if a user is on image783 and votes on the image and gives that image a "7" as a rating , I want to add "7" to that row in "totalVotes" of image783 on parse so that this image now has an additional vote of 7 added to the average/total, ultimately effecting the total average of that image and the total votes that the image received (i would calculate the average in the Xcode file). The total # of votes and the average votes from ALL users would ultimately be shown on the UI by being queried from Parse.