Realm object has been deleted or invalidated

后端 未结 12 1117
粉色の甜心
粉色の甜心 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 09:03

    In my experience, if you trying to use target object (which you wanna delete) after delete, application will crash.

    If you wanna trigger some code blocks after removing realm object, just trying to trigger that block right before the object removing in the memory. Trying to usage that object after removed from memory, will make some problem and will crash the app.

    For example:

        try! realm.write {
            print("deleted word: \(targetObject.word)")
            realm.delete(targetObject)
    
            //  targetObject was removed, so don't try to access it otherwise you gonna got the 'nil' value instead of object.
        }
    

提交回复
热议问题