realm-dotnet: UWP: The view is not in sync with the database.

Goals

Hello. I’m getting started with Realm platform to make an application in sync.

The problem is that the data in my view does not become synced with db and I need to fetch it again.

Expected Results

I want to add a data to database and I want the data list to be in sync with DB. I bonded the ListView to an IRealmCollection.

Actual Results

When I add the data in the db using

var todo = new Todo()
            {
                Id = DateTime.UtcNow.Ticks.ToString(),
                Subject = DateTime.Now.ToString(),
                Detail = Details.Text,
                Status = 2
            };
            RealmContext.Instance.Write(() =>
            {
                RealmContext.Instance.Add(todo);
            });

it saves successfully but the point is that UI does not update itself with newly added data; so I need to fetch the data again with RealmContext.Instance.All<Todo>().AsRealmCollection(); which is not what I want. When I subscribe to changes via

TodayList.CollectionChanged += (s, e) =>
            {
                foreach (var item in TodayList)
                {
                    Debug.WriteLine(item.Subject);
                }
            };

the fun fact is that it is not triggered when I insert my first data into the db. it triggers second time. Even more fun is this, the VS debug output shows all items that I’ve added except the one that it didn’t trigger this event !!! Please help.

Code Sample

You can take a look at the project that I’m working on at HERE

In the core project there are everything related to the Database. Also TimelineViewModel.cs file is related to the issue.

image

Views>Add>Task.xaml is for adding an item to DB.

Version of Realm and Tooling

  • Realm Object Server Version: Last version. On the cloud
  • Client SDK Version: Windows 10 SDK 17134
  • Client OS & Version: 3.1.0

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (6 by maintainers)

Most upvoted comments

I believe the issue is that you’re applying an ordering to the Passwords list, which snapshots the collection (i.e. it is no longer live). You can confirm that by trying to cast Database.PasswordGroup.Passwords.OrderBy(p => p.Website) as INotifyCollectionChanged - I’m fairly certain that’ll give you null.

If you want to preserve the collection as a live collection emitting notifications, you need to construct a query and apply the ordering on top of that (note that you’ll be limited by the sorting capabilities of the database, though a simple sort like that will work just fine). I haven’t tested it, but I think something like this would work:

binding.Source = Database.PasswordGroup.Passwords.AsRealmQueryable().OrderBy(p => p.Website);

This did indeed work, thanks a lot !