3

Using ReactiveUI 6.0.0 and having

var myReactiveList = new ReactiveList<SomeType>();

// [...] later I subscribe to the list:

myReactiveList.Changed.Subsribe(_ => 
{
     // this will be invoked 10 times, once for each item
     // in "myList" added through AddRange(...) below 
     // eg. count will increase from 1 to 10 on each call
     var test = myReactiveList.ToList();
     int count = test.Count;
});

// Now I add a couple of items at once 
// (eg. aList is a List<SomeType> with say 10 items)
// This will fire 10 times
myReactiveList.AddRange(aList);

Question: Is this expected behaviour? And if so, what should I do instead to subscribe so that my observer only gets notified once if a collection (AddRange(..)) is added?

Edit: Setting myReactiveList.ResetChangeThreshold = 0; did the trick.

Edit 2: There IS actually documentation for this (and other reactive stuff), just buried in ReactiveUI's docs branch, and in the docs folder in that branch: ReactiveList

So, in order to achieve what I originally wanted, I would do this:

using (myReactiveList.SuppressChangeNotifications()) {
    myReactiveList.AddRange(aList);
}

1 Answer 1

3

This is controlled by the ResetChangeThreshold property. By default, change suppression will only be triggered in AddRange if you add more than 10 items and the number of new items is 30% of the original. For details, see the source.

3
  • @cardinal Just note that it still will fire each item if you only add 9 items :) Jul 24, 2014 at 0:08
  • ups. In my real code I actually had 44 items, not 10 (I just took the number 10 at random, for this sample code... I'll take a closer look to ReactiveList's source then :-)
    – codeclash
    Jul 24, 2014 at 0:17
  • 1
    Yep - this is an optimization designed for views that bind to the list; if the list changes too much, we should send a single Reset to tell the view to reload the entire list from scratch instead of processing multiple Adds
    – Ana Betts
    Jul 24, 2014 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.