Subscribe for notification is not working

Realm Sdk: realm-dotnet version 10.15.1
Environment: Xamarin Forms

Hello,
I am working with an app where I need to manage multiple pages of the same collection. But the problem is that if I use single collection for all pages it is working fine. but when I use multiple collections and add their reference in the list to keep a reference and keep the listener alive, It does not work. Following is the code example:

        //To keep pages list reference
        private ObservableCollection<IQueryable<ChatMessage>> messagePages = new 
        ObservableCollection<IQueryable<ChatMessage>>();
        //To keep listener reference and dispose it later
        private List<IDisposable> listeners = new List<IDisposable>();

        public async Task GetMessagePageAsync(string conversationId, DateTimeOffset startDate, DateTimeOffset endDate)
        {
            var messageDb = new MessageDB();
            var messagesPage = await messageDb.GetMessagesForTimePeriod(conversationId, startDate, endDate);
            if (messagesPage != null)
            {
                messagePages.Add(messagesPage);
                var listener1 = messagePages[messagePages.Count() - 1].SubscribeForNotifications(RealmChangeListener);
                listeners.Add(listener1);
            }
        }

Following is the MessageDb.GetMessagesForTimePeriod:

        internal async Task<IQueryable<ChatMessage>> GetMessagesForTimePeriod(string conversationId, DateTimeOffset startDate, DateTimeOffset dayDate )
        {
            //Getting realm instance
            Realm realm = await _dbService.GetDbInstanceAsync();
            return realm.All<ChatMessage>().Where(o => o.ConversationID == conversationId && o.Timestamp >= dayDate && o.Timestamp <= startDate);
        }

Hi @Ahmad_Pasha, thanks for your message.

Just to be sure I got everything correctly, the issue here is that RealmChangeListner is not invoked, am I correct?
If so, could it be that the listeners collection is getting collected when moving to another page maybe?

@papafe Thanks for reply,
Even If I don’t change the page, It still doesn’t work except for one time when the listener is set.
And could be called second time if new changes arrives during the first call.

I see. A couple of things:

  • It would be convenient if you could update to a newer version of Realm. Version 10.18 introduces Source Generator but has an issue with UI bindings for unmanaged objects, so if you are using that you can use version 10.17
  • I can see you are using Xamarin.Forms. Is this happening on multiple platforms or only some? If you’re trying only on one platform, can you try on another one? Maybe it’s happening only on iOS for you, and it would be good to know if it’s happening also on Android, for instance.
  • How did your code look like when you were using only a single collection?