Get object id on object creation (Parse.com)
I would like to set a string value for the object id when I create the below object. At the moment it is printing "zero". Any ideas? Thanks to
var date:NSDate = currentDatePicker.date
var DOB:NSDate = DOBPicker.date
var user = PFUser.currentUser()
var report = PFObject(className:"reports")
report["name"] = nameTextField.text
report["DOB"] = DOB
report["date"] = date
report["user"] = user
report.saveInBackground()
var ojId = report.objectId
println(ojId)
+3
source to share
1 answer
saveInBackground
is an async method, so it doesn't wait. The reason the id is not set is because the object has not yet been saved.
I think you should use saveInBackgroundWithBlock:
and get the form of an object id in a block (which is executed after the object is saved):
var report = PFObject(className:"reports")
report["name"] = nameTextField.text
report["DOB"] = DOB
report["date"] = date
report["user"] = user
report.saveInBackgroundWithBlock { (success, error) -> Void in
if success {
var ojId = report.objectId
println(ojId)
}
}
or perhaps also save
, which runs synchronously, but don't use this from the main thread:
report.save()
var ojId = report.objectId
println(ojId)
+10
source to share