I'm not sure this will be a popular offer, but you can lazily create and subscribe to your collection. Then the first access to NwCustomers from the UI thread will completely remove everything.
public AlphaProductesVM (){} public ObservableCollection<Alphabetical_list_of_product> NwCustomers { get { if(_NwCustomers == null) { _NwCustomers = new ObservableCollection<Alphabetical_list_of_product>(); var repository = new NorthwindRepository(); repository .GetAllProducts() .ObserveOn(SynchronizationContext.Current) .Subscribe(AddElement); } return _NwCustomers; } }
or, if you introduce a UI thread manager into your view model, you can subscribe to it in the constructor.
var repository = new NorthwindRepository(); repository .GetAllProducts() .ObserveOn(theUIdispatcher) .Subscribe(AddElement);
source share