Search code examples
iosswiftparse-platformpfquerypfobject

Issues with changing existing Parse data


I am working with Parse for the first time in my application, and everything seems to be working well with the exception of when I go to change existing data. I am simply trying to change a string value that I have stored in a column of one of my items.

This is the code I currently have:

func sendTimeToParse() {
        var query = PFQuery(className: "ClassName")
        query.whereKey("Name", equalTo: rideNamePassed)
        query.getFirstObjectInBackgroundWithBlock {
            (object: PFObject?, error: NSError?) -> Void in
            if error != nil {
                println("The getFirstObject request failed.")
            } else {
                // The find succeeded.
                let object = PFObject(className: "ClassName")
                object.setValue(self.timeSelected, forKey: "WaitTime")
                object.saveInBackground()

                println("Successfully retrieved the object.")
            }
        }
    }
}

At the moment it just seems to create a new row of data and saves the time to that, however obviously I would like it to change the existing data in whatever row matches the name of the current record.

Anyone have any suggestions?


Solution

  • The problem is that you are creating a new PFObject with the line let object = PFObject(className: "ClassName") instead of using the retrieved object which is given as a parameter.

    Simply delete the line let object = PFObject(className: "ClassName") and unwrap the received optional. It could look something like the following:

    func sendTimeToParse() {
        var query = PFQuery(className: "ClassName")
        query.whereKey("Name", equalTo: rideNamePassed)
        query.getFirstObjectInBackgroundWithBlock {
            (object: PFObject?, error: NSError?) -> Void in
            if error != nil {
                println("The getFirstObject request failed.")
            } else {
                if let obj = object {
                    obj.setValue(self.timeSelected, forKey: "WaitTime")
                    obj.saveInBackground()
                }
                println("Successfully retrieved the object.")
            }
        }
    }