runceel / ReactiveProperty

ReactiveProperty provides MVVM and asynchronous support features under Reactive Extensions. Target frameworks are .NET 6+, .NET Framework 4.7.2 and .NET Standard 2.0.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Collection update

FoxTes opened this issue · comments

Hello. A question about the work of collections.
I have previously used RX UI.

Every time the NavigationSearchText changes, I have to get a new collection, but in my case I get the old collection plus the new one (adding element). I used a crutch like (.Do (_ => NavigationSearchItems? .ClearOnScheduler ())).

            NavigationSearchItems = NavigationSearchText
                .Skip(1)
                .Throttle(TimeSpan.FromMilliseconds(500))
                .Select(term => term?.Trim())
                .DistinctUntilChanged()
                .Do(_ => NavigationSearchItems?.ClearOnScheduler())
                .SelectMany(x => nameMenuItems.Where(y => y.Contains(x, StringComparison.OrdinalIgnoreCase)))
                .ToReactiveCollection();

Please, tell me how to do it better.

Probably, change this.


.ToReactiveCollection();

👇

.ToReadOnlyReactivePropertySlim();

This change does not work. The logic of work is changing.
The last element is selected and broken down by letters.

For example, instead of the "Home" element, we get "H", "O", "M", "E".

OK, how about this.
NavigationSearchItems type is changed.

NavigationSearchItems = NavigationSearchText
                .Skip(1)
                .Throttle(TimeSpan.FromMilliseconds(500))
                .Select(term => term?.Trim())
                .DistinctUntilChanged()
                .Select(x => nameMenuItems
                   .Where(y => y.Contains(x, StringComparison.OrdinalIgnoreCase))
                   .ToArray())
                .ToReadOnlyReactivePropertySlim();

Yes. It works. Thanks for the help.

But still. Better why does the collection have this behavior? Is it better to use properties or a collection in this case?

@FoxTes
I guess it works replacing to Clear() from ClearOnScheduler() and adding ObserveOnUIScheduler() after DistinctUntilChanged().

NavigationSearchItems = NavigationSearchText
                .Skip(1)
                .Throttle(TimeSpan.FromMilliseconds(500))
                .Select(term => term?.Trim())
                .DistinctUntilChanged()
                .ObserveOnUIDispatcher()
                .Do(_ => NavigationSearchItems?.Clear())
                .SelectMany(x => nameMenuItems.Where(y => y.Contains(x, StringComparison.OrdinalIgnoreCase)))
                .ToReactiveCollection();

However, I recommend @soi013 's approach. Because you said "Every time the NavigationSearchText changes, I have to get a new collection,", I guess it means ReadOnlyReactiveProperty<string[]>.

Thanks for the explanation. I will take this into account in the future.