Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InvalidOperationException when removing databound object #1903

Closed
carlst99 opened this issue Sep 20, 2019 · 5 comments · Fixed by #2575
Closed

InvalidOperationException when removing databound object #1903

carlst99 opened this issue Sep 20, 2019 · 5 comments · Fixed by #2575
Assignees

Comments

@carlst99
Copy link

carlst99 commented Sep 20, 2019

Goals

I have a WPF application in which I am binding a ListView to a collection of RealmObjects. My goal is to remove one of these objects from the database

Expected Results

I expected the object to be successfully removed and the bound ListView to update.

Actual Results

System.InvalidOperationException: 'Added item does not appear at given index '1'.'. No stack trace is given. On enabling native code debugging the exception seems to be occuring in realm-wrappers.dll. Slightly misleading as the debug output shows the exception occurs in PresentationFramework.dll

Steps to Reproduce

Bind a ListView to a collection of RealmObjects. Remove an object from the realm instance and you will cause an InvalidOperationException

Code Sample

I've created a repository with a couple of test projects highlighting the issue. Add a few blogs and then select and try to delete one; it should crash with the error.

Note that the first test project mimics my use-case scenario, hence the libraries Stylet and MaterialDesignThemes

Version of Realm and Tooling

  • realm-dotnet 4.1.0, local DB
  • Have tested on .NET Framework 4.6.1, 4.8
  • Windows 10
@carlst99
Copy link
Author

I've figured out how to workaround this. The full example is in the workaround branch of the sample repo (RealmTesterBasic project), but here's a summary.

Instead of binding directly to an IQueryable, bind to a list, e.g.

List<T> MyCollection = _realm.All<T>().ToList()

Then everytime you update the DB, simply reset the list using the above. You can capture updates either through your own logic or with the Realm.RealmChanged event.

The same goes for any IList properties in your RealmObjects; convert them to a list in your ViewModel before binding, and update them as appropriate. It's painful, but it works.

@redbeaner
Copy link

Hi ! I'm facing the same issue and just wonder why can't we use IEnumerable return from _realm.All instead have to call ToList(). The Example from Realm.Net on Github for Xamarin Form also suggest to bind directly to IEnumerable.

@papafe
Copy link
Contributor

papafe commented Aug 19, 2021

I've figured out how to workaround this. The full example is in the workaround branch of the sample repo (RealmTesterBasic project), but here's a summary.

Instead of binding directly to an IQueryable, bind to a list, e.g.

List<T> MyCollection = _realm.All<T>().ToList()

Then everytime you update the DB, simply reset the list using the above. You can capture updates either through your own logic or with the Realm.RealmChanged event.

The same goes for any IList properties in your RealmObjects; convert them to a list in your ViewModel before binding, and update them as appropriate. It's painful, but it works.

Yes, it definitely works, but as you said this is a problem as it will create issues with the bindings. I manage to reproduce the issue and I'll try to investigate

@papafe papafe self-assigned this Aug 19, 2021
@polymath74
Copy link

I have recently started converting a project from SqlServer to Realm, and have just encountered this bug. I can reproduce it consistently. I added my own CollectionChanged event handler, and my guess is WPF is misusing the event args. (I have had problems with WPF and CollectionChanged events before!)

So the notification comes in like this:
image

It has 1 old item, with the index in OldStartingIndex, but the item itself is null.

I wonder whether WPF is trying to look for a null object at that index? Or is it just completely wrong—the exception complains about an "added item" but this is a delete…?

@nirinchev
Copy link
Member

Yeah, we were looking into that Friday and came across the culprit, which, ironically, has a comment about replacing the message with a better one 🥲 In any case, we have several avenues we're exploring that will mitigate this and hope to have a fix with our next release.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants