realm / realm-dotnet

Realm is a mobile database: a replacement for SQLite & ORMs

Home Page:https://realm.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IRealmCollection SubscribeForNotifications not working!!!

GoodWayDev opened this issue · comments

What happened?

I used SubscribeForNotifications with IRealmCollection. After packaging the app into an MSIX for installation, I didn't receive any notifications when adding a new record. However, during local debugging, I could receive notifications.

Repro steps

  1. Implement SubscribeForNotifications on ViewModel init.
  2. Add a new record.
  3. SubscribeForNotifications didn't receive any notifications.

Version

8.0

What Atlas Services are you using?

Local Database only

What type of application is this?

Other

Client OS and version

Windows 10 22H2 build 19045.4291

Code snippets

DataService.cs

public class DataService
{
    public static DataService Instance { get; } = new DataService();

    private readonly Realm LocalRealm;

    public IRealmCollection<Device> DeviceList { get; }

    public async Task<Device> AddDevice(Device device)
    {
        return await LocalRealm.WriteAsync(() =>
        {
            return LocalRealm.Add(device, true);
        });
    }
}

ViewModel.cs

public partial class DeviceListViewModel : ObservableObject
{
    public ObservableCollection<Models.Device> DeviceList { get; } = [];

    private IDisposable deviceListNotifyToken;

    public DeviceListViewModel()
    {
        deviceListNotifyToken = DataService.Instance.DeviceList.SubscribeForNotifications((list, changeSet) =>
        {
            if (changeSet != null)
            {
                foreach (var idx in changeSet.InsertedIndices)
                    DeviceList.Insert(idx, list[idx]);

                foreach (var idx in changeSet.ModifiedIndices)
                {
                    var item = list[idx];
                    DeviceList.RemoveAt(idx);
                    DeviceList.Insert(idx, item);
                }

                foreach (var idx in changeSet.DeletedIndices)
                    DeviceList.RemoveAt(idx);
            }

            OnPropertyChanged();
        });
    }
}

Stacktrace of the exception/crash you're getting

No crash.

Relevant log output

No log.

➤ PM Bot commented:

Jira ticket: RNET-1144

How is DeviceList populated and stored on the DataService object? Additionally, is there a reason why you're copying the objects from Realm into the observable collection? Why not just use the realm collection directly?

The DeviceList on the DataService is populated and stored by calling the AddDevice() from the DeviceManager class, which detects devices.
The reson why did not use IRealmCollection is because I don't want the ViewModel import Realm. But I've also tried with realm collection and the problem persists.

Models.Device already imports Realm transitively, so unless you project that into a view model, you're still ending up with a Realm dependency.

In any case, I've tried creating a simple project (I only built it in release, haven't packaged it in MSIX) and can't reproduce the behavior - can you create a repro case that we can use to further investigate.

Thanks, I got it.
It only occurs after being packaged into an MSIX installer. During the development, build in either debug or release works fine. It's only when packaging it into an MSIX for the client that this issue arises.

I tried packaging a simple WPF app in MSIX and wasn't able to repro the issue you're describing. I've attached a zipped version of the project I created - I can package that, install it, and it works as expected - clicking the "Add item" button will immediately update the listview. If you can confirm whether it works for you and/or modify it so that it reproduces the problem, I can continue looking into it.

Dotnet-3598.zip