Swift Remove Object from Realm

后端 未结 6 1240
庸人自扰
庸人自扰 2021-02-04 04:06

I have Realm Object that save list from the JSON Response. But now i need to remove the object if the object is not on the list again from JSON. How i do that? This is my init f

6条回答
  •  甜味超标
    2021-02-04 04:58

    I will get crash error if I delete like top vote answer.

    Terminating app due to uncaught exception 'RLMException', reason: 'Can only add, remove, or create objects in a Realm in a write transaction - call beginWriteTransaction on an RLMRealm instance first.'
    

    Delete in a write transaction:

    let items = realm.objects(Items.self)
    try! realm!.write {
        realm!.delete(items)
    }
    

提交回复
热议问题