查询后写入Parse列 - Swift

时间:2015-08-18 14:03:06

标签: ios swift parse-platform

如何使用查询从Parse类中查找currentUser,然后将数据写入该用户列?

    var query = PFQuery(className:"User")
    query.whereKey("objectId", equalTo:PFUser.currentUser()!)
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]?, error: NSError?) -> Void in

        if error == nil {
            // The find succeeded.
            println("Successfully retrieved \(objects!.count) scores.")
            // Do something with the found objects
            if let objects = objects as? [PFObject] {
                for object in objects {
                    println(object.objectId)

                }
            }
        } else {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        }
    }

2 个答案:

答案 0 :(得分:2)

更改此行

query.whereKey("objectId", equalTo:PFUser.currentUser()!)

要:

query.whereKey("objectId", equalTo:PFUser.currentUser().objectId)

如果您从解析用户类查询,它应该是 _User 而不是用户

仅保存数据

 var ObjectToSave = PFObject(className: "_User")
 ObjectToSave["raw"] = "whateveryoulike"
 ObjectToSave["UserId"] = PFUser.currentUser()?.objectId // this piece of code is when you create a new class 
    ObjectToSave.saveInBackgroundWithBlock { (success:Bool, error:NSError?) -> Void in
        if error == nil{
          println("data was saved")
        }
        else
        {
        println("error")
        }
    }
}

我认为将其他数据保存到_User类是个好主意,您应该将此类留给登录或注册。您应该创建一个新类,然后使用userid保存所有新数据......

答案 1 :(得分:0)

您无需查询当前用户。 使用此

var user = PFUser.currentUser()
user["one"] = "whateveryoulike"
user["two"] = "whateveryoulike"

 user.saveInBackgroundWithBlock { (success:Bool, error:NSError?) ->  Void in
    if error == nil{
      println("data was saved")
    }
    else
    {
    println("error")
    }
}

}