Realm object has been deleted or invalidated

后端 未结 12 1105
粉色の甜心
粉色の甜心 2020-12-13 08:20

When I start my app, I perform an API call to see whether there\'s new data available. The data is stored in my local Realm database, and some of it is displayed in the init

12条回答
  •  一向
    一向 (楼主)
    2020-12-13 08:52

    In my case I was deleting data from 2 tables at once.. one with a foreign to the other.

            let itemToDelete = counters[indexPath.row]
            let realm = try! Realm()
            try! realm.write {
                realm.delete(itemToDelete)
                let predicate = NSPredicate(format: "counterid = \(c.id)")
                let children = realm.objects(Details.self).filter(predicate)
                realm.delete(children)
            }
    

    But the problem was that I was trying to delete the children of the item that does not exist anymore. Switching the order, solved it!

            let itemToDelete = counters[indexPath.row]
            let realm = try! Realm()
            try! realm.write {
                let predicate = NSPredicate(format: "counterid = \(c.id)")
                let children = realm.objects(Details.self).filter(predicate)
                realm.delete(children)
                realm.delete(itemToDelete) //this should be deleted after
            }
    

    Hope this helps someone else!

提交回复
热议问题