【问题标题】:swift: Update parse object without knowing his objectIdswift:在不知道他的 objectId 的情况下更新解析对象
【发布时间】:2015-07-29 11:22:29
【问题描述】:

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

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

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

我已经有了这个 sn-p 代码:

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

【问题讨论】:

    标签: xcode swift parse-platform


    【解决方案1】:

    假设您的“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
        }
    }
    

    【讨论】:

    • 感谢您的回复。所以它看起来像这样? var query = PFQuery(className: "UserProfiles") var userId = PFUser.currentUser()!.objectId query.whereKey("userObjectId", equalTo: userId) //在这里你只会找到结果 query.findObjectsInBackgroundWithBlock {(objects: [AnyObject]?, error: NSError?) -> Void in if error == nil { query["name"] = "new Name" query.saveInBackground() } else { //you have an error } }
    • 从“UserProfiles”类接收到对象后,您打算做什么?
    • 在 mysql 中看起来像 query("UPDATE UserProfiles SET name = "New Name" WHERE UserObjectId = $user"); - 希望你能理解?!
    • 我想为一个具有唯一 userObjectId PFUser.currentUser().objectId 的特殊用户添加列和/或更新行
    • 我更新了我的答案,希望能帮助你。
    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-24
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多