Unable to delete an object in a realm database

我们两清 提交于 2019-12-11 02:33:11

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!