realm-swift: Realm object has been deleted or invalidated

I get this error after I have deleted an object and try to create another object in its place:

“Realm object has been deleted or invalidated”

I am using the following code to create/recreate the object:

try! realm.write { realm.create(ChatMessageRealm.self, value: realmChatMessage, update: true) }

And I am deleteing the object with this code:

try! realm.write { try! realm.delete(message) }

This is how it is explained in the docs but I am still getting the error. Does anyone have an idea how to solve this?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 28 (9 by maintainers)

Most upvoted comments

@jpsim there is no confusion on that. It is clear that accessing a deleted object should result in an exception. The problem is that after objects are deleted, they shouldn’t be showing up in result sets when querying realm right after objects are deleted, which is exactly what is happening here. Does this make more sense?

Wanted to circle back on this. Using the realm notification system removed the bug I was having entirely as the results passed from the realm notification block always had valid objects. That being said, it is definitely clear that after deleting objects from realm and calling

[realm commitWriteTransaction]

there is a small window right after the commit where if you query for objects that would have shown up in the result set had you not deleted those previous objects, you might get objects in the result set that are invalidated or deleted. It is not directly reproducible as I could never reproduce it myself but it was definitely showing up in the crash reports a pretty significant amount.

Just curious, when you add/delete an object form realm do you instantiate Realm every time or do you define a Realm object once and use a reference to that Realm?

For example, would you use:

Realm().objects(AnyObject) Realm().objects(SomeOtherObject)

or:

var realm: Realm?; realm = try! Realm(); realm.objects(AnyObject); realm.objects(SomeOtherObject);

I get the same error for similar condition.

I have issue like this, my code was like this

let realm = try! Realm() let cartItems = realm.objects(CartRealm.self).filter(queryString) ** try! realm.write { realm.delete(cartItems) } **

and when i changed the line between asterisk that writing on db into like this

realm.beginWrite() realm.delete(cartItems) try! realm.commitWrite()

the crash was gone.

Hey sorry for the late response I have been very busy lately.

I solved this by Just incrementing through the Results object that Realm returns directly and putting it into the table instead of throwing the data into a swift array and then handling it. This made most of my invalidation errors go away. I also was using a global variable to reference a realm object. This ended up causing a lot of errors so Instead I just saved the ID of the object and grab it from Realm every time I need to read or update it.

After these two changes I haven’t run into any errors. I hope this helps someone out there!

Exactly what is happening for me. I retrieve a set of realm objects, put them into an array, display info from them in a TableViewController. Then I clear out the array, delete the objects from Realm, and next time I try to add an object in the array and access it it throws me that error. It is as if the objects are not being delete properly