我希望有人能帮助我。我没有使用 Parse,Google 提取的所有结果都是与 Parse 相关的答案。我有一些代码可以将实体保存到核心数据中。我想在保存后立即抓取与项目关联的对象 ID。有什么办法可以做到这一点吗?请参阅下面的代码片段以了解我在说什么:
var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
var context:NSManagedObjectContext = appDel.managedObjectContext!
let entity = NSEntityDescription.entityForName("Aquarium", inManagedObjectContext: context)
//Instance of our custom class, reference to entity
var newAquarium = UserAquariums(entity: entity!, insertIntoManagedObjectContext: context)
newAquarium.aquariumName = "Name of aquarium"
context.save(nil)
println("Context saved.")
//Here is where I have to figure out how to get the object ID of the aquarium directly after I saved it to the context.
var aquariumObjectID: String
aquariumObjectID = ??????????
最佳答案
newAquarium.objectId
是的,请确保先将其保存到数据库中。
我注意到您正在使用同步执行的保存。不要在主线程中使用它。
关于ios - 保存上下文后立即获取对象 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31642156/