问题
I'm unable to delete an object in a realm database: I have following exception:
0x108196d6f: movq 0x202112(%rip), %rcx ; "exceptionWithName:reason:userInfo:"
0x108196d76: leaq 0x1e6d0b(%rip), %rdx ; @"RLMException"
0x108196d7d: leaq 0x1e71c4(%rip), %rbx ; @"Unable to delete an object not persisted in this Realm."
The record is in the database when I check with realm browser!!! Who had the same problem?
this is my code:
func deleteDatabase() {
let realm = RLMRealm.defaultRealm()
realm.beginWriteTransaction()
for result in deleteUoMArray {
var item = result as UoM
var uomObject = UnitOfMeasurement()
uomObject.guid = item.guid
uomObject.unitOfMeasurement = item.unitOfMeasurement
uomObject.selected = item.selected
uomObject.index = item.index
realm.deleteObject(uomObject)
}
realm.commitWriteTransaction()
}
回答1:
If your goal is to delete all objects in the database, as the name of the deleteDatabase function would imply, all you need to do is call:
let realm = RLMRealm.defaultRealm()
realm.beginWriteTransaction()
realm.deleteAllObjects()
realm.commitWriteTransaction()
Otherwise, if deleteUoMArray is an Array, RLMResults, or RLMArray of objects to delete, you can call realm.deleteObjects(deleteUoMArray).
来源:https://stackoverflow.com/questions/28243034/unable-to-delete-an-object-in-a-realm-database