Renato Pimpão
Renato Pimpão

Reputation: 271

Write to a Parse column after query - Swift

How can I use a query to find the currentUser from a Parse Class and then write data into that user columns?

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

Upvotes: 0

Views: 273

Answers (2)

Lamour
Lamour

Reputation: 3040

Change this line

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

To:

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

And also if you are querying from the parse user class it should be _User not User

To Only save data

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

I don't think it is a good idea to save other data into the _User class, you should leave this class for the login or sign up. You should create a new class then save all new data with the userid...

Upvotes: 2

kader kabore
kader kabore

Reputation: 345

you don't need to query for the current user. Use this

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

}

Upvotes: 0

Related Questions