在 Observable 项目生成时处理它们 [英] Dispose of Observable Items as they are generated

查看:24
本文介绍了在 Observable 项目生成时处理它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IObservable 可以生成一次性物品,并且在其生命周期内可能会生成无限数量的物品.正因为如此,我想在每次生成新项目时处理最后一个项目,所以 使用 操作符对此不起作用.是否有不同的 Rx.NET 运算符可以完成此功能?

I have an IObservable that generates items that are disposable, and it will generate a potentially infinite number of them over its lifetime. Because of this, I want to dispose of the last item each time a new item is generated, so the Using operator will not work for this. Is there a different Rx.NET operator that can accomplish this function?

推荐答案

如果你有一个 IObservablesource 然后执行此操作以自动处理先前的值并在序列结束时进行清理:

If you have a IObservable<IDisposable> source then do this to automatically dispose of the previous value and to clean up when the sequence ends:

IObservable<IDisposable> query =
    Observable.Create<IDisposable>(o =>
    {
        var serial = new SerialDisposable();
        return new CompositeDisposable(
            source.Do(x => serial.Disposable = x).Subscribe(o),
            serial);
    })

这篇关于在 Observable 项目生成时处理它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆