MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Deleting From RealmDB - XamarinForms

Hello , I’m having problem when deleting from Realm, I’m using Xamarin Forms with ReactiveUI. Adding , Updating works fine , but Deleting not , I’m getting Realms.Exceptions.RealmInvalidObjectException: 'Attempted to access detached row' , I have a service class that have all methods for CRUD operations . There I’m creating instance of Realm object. My service class is singleton.

@Dragan_Blanusa I think you need to use .NET’s .freeze() method for this, especially for ReactiveUI. You can see a code snippet here:
https://developer.mongodb.com/article/realm-database-and-frozen-objects

A more detailed example can be seen here -

Its hard to say without seeing your code but this will likely help you

What should I do with frozen object if I can’t write and subscribe to it ?? My problem is when I remove something from server side and my mobile app is in background , and goes from background to foreground , app just crash with exception I wrote .

Can you isolate a small repro case that we can use to investigate the issue? Generally what happens is that when an object is deleted, the collection containing the object emits a collection changed event that will tell the container to remove an element. Unfortunately, some binding engines will attempt to read data from the removed item prior to the removal, which then results in RealmInvalidObjectException. We have some custom logic to handle this when using Xamarin.Forms:

Essentially, what this does is provide a custom property getter that the XF binding engine uses that will return the property’s default value instead of throwing an exception, if the object is deleted. I imagine whatever framework you’re using for ReactiveUI is doing things slightly differently, which is why it doesn’t go through that piece of code. In any case, having a small repro project will allow us to explore ways to support it.

@Dragan_Blanusa You can open an issue in this repo if you are are able to provide a reproduction case:

1 Like