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
我无法获得objectId,我怎么能得到它? 非常感谢你提前。
答案 0 :(得分:6)
正如您的函数名称所说,它是一个名为asynchron的函数。这意味着主线程不会等待函数完成。所以你会得到一个(仍然)空的objectId。
要在saveInBackground完成后获取您的objectID,您必须将获取ID的行放入if子句中。
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)
}
}
另一种选择是在主线程中调用save()
。这样,函数在调用objectId之前就完成了。 Parse并不推荐这样做,但它有可能:
cardObject.save()
dbId = cardObject.objectId