question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
BergHeisencommented, Jan 18, 2023

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 !

0reactions
nirinchevcommented, Jan 18, 2023

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 will convert the list to a query and execute the sort at the database level, thus preserving change notification capabilities.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UWP Data Binding with EF7 (EF Core)
I've written a simple Windows 10 UWP application that presents the user with a ListView of data bound to a SQLite database via...
Read more >
Add offline data sync to your Windows (UWP) app
Offline sync allows end users to interact with a mobile app—viewing, adding, or modifying data—even when there's no network connection. Changes ...
Read more >
OneNote UWP sync problem
I've tried renaming the page and moving it to a different section but it still won't sync, even though other pages sync fine....
Read more >
Where did the UWP version go? [Will be returned in ...
So my demo of the 1Password UWP features convinced him to get an account also. But to our shock the 1Password windows 10...
Read more >
Realm DB and windows phone 10
I want to install realm database on windows mobile phone version 10 using UWP by c#. My environment : Microsoft Visual Studio Professional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found