swift:更新解析对象而不知道他的objectId

时间:2015-07-29 11:22:29

标签: xcode swift parse-platform

我有一个userSignUp和登录系统,其中存储了用户的电子邮件地址和密码。

后来我想向这个用户添加更多信息,所以我在parse.com上创建了一个名为“UserProfile”的新数据类,其中我存储了已注册用户的userObjectId。

所以现在我想在数据类UserProfile中更新这个用户,但我现在只是来自UserClass中用户的objectId。 所以我想更新像“更新用户userObjectId = PFUser.currentUser()。objectId”

我已经有了这段代码:

    var query = PFQuery(className:"UserProfiles")
    var userId = PFUser.currentUser()?.objectId?
    query.whereKey("userObjectId", equalTo: userId)
    //i don't think, that the following code is correct
    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()
        }
    }

1 个答案:

答案 0 :(得分:0)

假设您的“userObjectId”字段是字符串字段而不是指针字段,那么您可以执行此操作。

var query = PFQuery(className: "UserProfiles")
var userId = PFUser.currentUser()!.objectId

query.whereKey("userObjectId", equalTo: userId)
//here you would just find the results
query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in
    if error == nil {
        //you have the user, assuming one but their could be more
        //this will only return one user since you're using the objectID as a field and those are unique, so...

        for user in objects! {
            user["aColumnYouWishToUpdate"] = whatYouWishToUpdateTo
            user["anotherColumnToUpdate"] = anotherUpdatedVariable
            //do this for any columns you want updated
            user.saveInBackground() //to save the newly updated user
        } 

    } else {
        //you have an error
    }
}