我正在使用 IObservable 在后台进行一些加载。我想指定何时完成加载。我怎样才能只做一次,而不是每次我的数据检索器都返回 yield ?我该怎么做?
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;});
最佳答案
你应该看看 Finally extension method ,它会完全按照您的意愿行事!
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));
这应该在可观察序列终止后执行。
关于c# - 在 Observable 完成后做点什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7982170/