简体   繁体   中英

Updating Objects in iOS Swift with Parse

I've a table 'preferences' where user preferences are saved along with username. I created this method to update current user's preference' but somehow it doesn't seem to work. I am not sure if the portion "prefQuery.getObjectInBackgroundWithId(object.objectId)" is required at all.

I am new to Parse, could somebody please help me point what could be the issue.

func userPreferences(){
    var currUser = PFUser.currentUser()
    var prefQuery = PFQuery(className: "preferences")
    var prefObj = PFObject(className: "preferences")

    if let currUserName = PFUser.currentUser()?.username {
        prefQuery.whereKey("username", equalTo: currUserName)
    }

    prefQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {

                    prefQuery.getObjectInBackgroundWithId(object.objectId){
                        (object: PFObject?, error: NSError?) -> Void in
                        if error == nil || object != nil {
                            prefObj["agestart"] = self.fromAge.text
                            prefObj["ageend"] = self.toAge.text
                            prefObj["location"] = self.location.text
                            ProgressHUD.showSuccess("Update successful")
                        } else {
                            ProgressHUD.showError("Update failed")
                        }
                    }
                }
            }
        }
    }
}

I found the issue and have updated my codes. The working codes are below; the issue was with the "prefObj["agestart"]" block of codes where I was using the wrong Query instance. You can compare the two snippets:

func userPreferences(){
    var currUser = PFUser.currentUser()
    var prefQuery = PFQuery(className: "preferences")
    var prefObj = PFObject(className: "preferences")

    if let currUserName = PFUser.currentUser()?.username {
        prefQuery.whereKey("username", equalTo: currUserName)
    }

    prefQuery.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            if let objects = objects as? [PFObject] {
                for object in objects {
                    prefQuery.getObjectInBackgroundWithId(object.objectId){
                        (prefObj: PFObject?, error: NSError?) -> Void in
                        if error != nil {
                            println(error)
                            ProgressHUD.showSuccess("Error while updating")
                        } else if let prefObj = prefObj {
                            prefObj["agestart"] = self.fromAge.text
                            prefObj["ageend"] = self.toAge.text
                            prefObj["location"] = self.location.text
                                ProgressHUD.showSuccess("Update successful")
                            prefObj.saveInBackgroundWithBlock({ (Bool, error: NSError!) -> Void in })
                        }
                    }
                }
            }
        }
    }
}

The best way to cut your code to the maximum, you can see below:

func userPreferences() {

        let prefQuery = PFQuery(className: "preferences")
        if let currUserName = PFUser.current()?.username {
            prefQuery.whereKey("username", equalTo: currUserName)
        }

        prefQuery.findObjectsInBackground {
            (objects, error)  in

            if error == nil {
                if let objects = objects {
                    for object in objects {
                                object["agestart"] = "ur value"
                                object["ageend"] = "ur value"
                                object["location"] = "ur value"
                                print("Update successful")
                                object.saveInBackground()
                    
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM