Do something after observable finishes?
-
19-02-2021 - |
Question
i am using a IObservable to do some loading in the background. i want to specify when this is finished loading. How can i do this only once, instead of every time my data retriver does a yield return? How do I do this?
bool IsLoading = true;
ObservableCollection<MyData> dataList = new
ObservableCollection<MyData>();
DataLoader.RetrieveData().ToObservable(Scheduler.ThreadPool).Select(x => x).ObserverOn(Scheduler.Dispatcher).Subscribe(x => {
dataList.Add(x);
IsLoading = false;});
Solution
You should look at the Finally extension method, it will do exactly what you want!
bool IsLoading = true;
ObservableCollection<MyData> dataList = new ObservableCollection<MyData>();
DataLoader.RetrieveData()
.ToObservable(Scheduler.ThreadPool)
.Select(x => x)
.ObserverOn(Scheduler.Dispatcher)
.Finally(() => IsLoading = false)
.Subscribe(x => dataList.Add(x));
That should execute after the observable sequence is terminated.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow