diff --git a/Aosta.Ava/Aosta.Ava/Extensions/RealmExtensions.cs b/Aosta.Ava/Aosta.Ava/Extensions/RealmExtensions.cs index 81c7a4f..cb6f9b3 100644 --- a/Aosta.Ava/Aosta.Ava/Extensions/RealmExtensions.cs +++ b/Aosta.Ava/Aosta.Ava/Extensions/RealmExtensions.cs @@ -25,19 +25,33 @@ public static class RealmExtensions /// The Realm projection. /// The returned subscription token. /// The type of the entities. - /// The to observe on. + /// The IChangeSet to observe on. public static IObservable> Connect(this IQueryable query, out IDisposable token) where T : IRealmObjectBase { return Connect(query.AsRealmCollection(), out token); } + /// + /// Convert the Realm list into an observable list and subscribe to the collection changes. + /// + /// The Realm list. + /// The returned subscription token. + /// The type of the entities. + /// The IChangeSet to observe on. public static IObservable> Connect(this IList list, out IDisposable token) where T : IRealmObjectBase { return Connect(list.AsRealmCollection(), out token); } + /// + /// Convert the Realm collection into an observable list and subscribe to the collection changes. + /// + /// The Realm collection. + /// The returned subscription token. + /// The type of the entities. + /// The IChangeSet to observe on. public static IObservable> Connect(this IRealmCollection collection, out IDisposable token) where T : IRealmObjectBase { @@ -56,20 +70,7 @@ public static IObservable> Connect(this IRealmCollection col } else { - cache.Edit(update => - { - // Handle deleted elements - foreach (int i in changes.DeletedIndices) - { - update.RemoveAt(i); - } - - // Handle inserted elements - foreach (int i in changes.InsertedIndices) - { - update.Insert(i, sender[i]); - } - }); + cache.Edit(update => updateCache(changes, update, sender)); logger.Debug("Processed {ChangesCount} changes for {Type} observable cache: [Removed: {Removed}, Added: {Added}, Moved: {Moved}]", changes.DeletedIndices.Length + changes.InsertedIndices.Length, @@ -82,4 +83,20 @@ public static IObservable> Connect(this IRealmCollection col return cache.Connect(); } + + private static void updateCache(ChangeSet changes, IExtendedList update, IRealmCollection sender) + where T : IRealmObjectBase + { + // Handle deleted elements + foreach (int i in changes.DeletedIndices) + { + update.RemoveAt(i); + } + + // Handle inserted elements + foreach (int i in changes.InsertedIndices) + { + update.Insert(i, sender[i]); + } + } }