使用Parse更新iOS Swift中的对象

时间:2015-06-02 09:27:03

标签: ios swift parse-platform

我有一张桌子'偏好'用户首选项与用户名一起保存的位置。我创建了这种方法来更新当前用户的偏好'但不知怎的,它似乎没有用。我不确定该部分" prefQuery.getObjectInBackgroundWithId(object.objectId)"完全是必需的。

我是Parse的新手,请有人帮我指出可能是什么问题。

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")
                        }
                    }
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我发现了问题并更新了我的代码。工作代码如下;问题在于" prefObj [" agestart"]"我使用错误的Query实例的代码块。您可以比较两个片段:

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 })
                        }
                    }
                }
            }
        }
    }
}

答案 1 :(得分:0)

最大限度地减少代码的最佳方法,您可以在下面看到:

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()
                    
                }
            }
        }
    }
}