var cardObject = PFObject(className: "YourCard")
cardObject["cardNumber"] = cardNumber
cardObject["balance"] = balance
cardObject["expire"] = date
cardObject["validFlg"] = cardStatus
cardObject.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if (success != nil) {
NSLog("Object created with id: \(cardObject.objectId)")
} else {
NSLog("%@", error)
}
}
dbId = cardObject.objectId
I couldn't get objectId, how can I get it? Thank you very much in advance.
As the name of your function already says, it is a function which is called asynchron. That means that the main thread doesn't wait for the function to finish. So you will get an (still) empty objectId.
To get your objectID after saveInBackground is finished, you have to put your line where you get the ID into the if-clause.
var cardObject = PFObject(className: "YourCard")
cardObject["cardNumber"] = cardNumber
cardObject["balance"] = balance
cardObject["expire"] = date
cardObject["validFlg"] = cardStatus
cardObject.saveInBackgroundWithBlock {
(success: Bool!, error: NSError!) -> Void in
if (success != nil) {
NSLog("Object created with id: \(cardObject.objectId)")
//Gets called if save was done properly
dbId = cardObject.objectId
} else {
NSLog("%@", error)
}
}
Another option would be to call save()
in the main thread. That way the function finishes before you call the objectId. Parse doesn't recommend that, but it's a possibility:
cardObject.save()
dbId = cardObject.objectId
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With