1

in my app Im trying to give the user points every time they create an event. I am setting up a PFQuery to retrieve the current score then saving the required points back to the class. My problem is that I can't update the score once it has been created so I need a way to "Update" the current score data with the added score.
This is my code:

// Give the User Points

    let saveScore = PFUser.currentUser()

    var query = PFQuery(className:"User")
    query.whereKey("score", equalTo: saveScore!)
    query.findObjectsInBackgroundWithBlock ({
        objects, error in

        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) scores.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {

                    let Score = object["score"] as! String

                    println(object.objectId)

                    let Points = ("100" + Score)


                    saveScore!.setObject(Points, forKey: "score")

                    saveScore!.saveInBackgroundWithBlock { (success: Bool,error: NSError?) -> Void in
                        println("Score added to User.");

                    }
                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    })

Can anyone help? Thanks

2 Answers 2

1

Since you already have the current user there's no reason to query it. However you should fetch it if needed to make sure you're working with the latest data. Once fetched set your score variable, add the 100 string and then save the updated score variable, like so:

if let currentUser = PFUser.currentUser() {

        currentUser.fetchIfNeededInBackgroundWithBlock({ (foundUser: PFObject?, error: NSError?) -> Void in

            // Get and update score

            if foundUser != nil {

                let score = foundUser!["score"] as! String

                let points = "100" + score

                foundUser!["score"] = points

                foundUser?.saveInBackgroundWithBlock({ (succeeded: Bool, error: NSError?) -> Void in

                    if succeeded {

                        println("score added to user")
                    }
                })

            }

        })

    }
Sign up to request clarification or add additional context in comments.

2 Comments

Ok sweet that works thanks! But now I have another problem in that it literally displays the score as 100+100=100100 since it is a String. How do I add it together so that 100+100=200? Im assuming I need to convert it into an Integer?
Please mark the answer as correct so others know that it worked. On SO each question should be specific to one issue. Though It sounds like you should be storing your data as Integers and not Strings
0

You need to query for the saved object and then just save like normal. It will update like so:

 var query = PFQuery(className:"GameScore")
 query.getObjectInBackgroundWithId("xWMyZEGZ") {
 (gameScore: PFObject?, error: NSError?) -> Void in
 if error != nil {
 println(error)
 } else if let gameScore = gameScore {
 gameScore["cheatMode"] = true
 gameScore["score"] = 1338
 gameScore.saveInBackground()
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.