使用反应式扩展来监视 IEnumerable

发布于 2024-12-02 16:25:22 字数 299 浏览 1 评论 0原文

我正在连接到一个对象,该对象将对象集合异步加载到 IEnumerable 中。在我连接时,IEnumerable 的集合中可能已有项目,并且可能会在应用程序的生命周期中添加项目,我需要在项目发生时收到通知。例如,它可以是包含银行交易列表的银行帐户。

挑战是这样的。我想将 IEnumerable 中初始值的处理与任何新添加的内容结合起来。它们目前是两个进程。我想完全消除 NotifyCollectionChanged 的​​使用。

我可以修改持有 IEnumerable 的后端。如果此问题存在其他解决方案,则它不需要保留为 IEnumerable。

I'm connecting to an object that asyncronously loads a collection of objects into an IEnumerable. At the time I connect, the IEnumerable may have items already in it's collection, and may add items during the lifetime of the application that I need to be notified of as they occur. As an example, it could be a bank account containing a list of bank transactions.

The challenge is this. I want to combine the processing of the initial values in the IEnumerable with any new additions. They are currently two processes. I would like to eliminate the use of NotifyCollectionChanged entirely.

I can modify the backend holding the IEnumerable. It does not need to remain as an IEnumerable if a solution to this question exists otherwise.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤舞 2024-12-09 16:25:22

我建议该对象不应该公开 IEnumerable,因为那是“冷可观察值”,在您的情况下,您需要一些可以在将来获得其他项目的东西。

对此进行建模的最佳方法是使用 ReplaySubject 而不是 IEnumerable。下面是一个演示与您类似的情况的示例:

//Function to generate the subject with future values
public static ReplaySubject<int> GetSubject()
{
    var r = new ReplaySubject<int>();
    r.OnNext(1); r.OnNext(2); r.OnNext(3);
    //Task to generate future values
    Task.Factory.StartNew(() =>
    {
        while (true)
        {
            Thread.Sleep(3000);
            r.OnNext(DateTime.Now.Second);
        }
    });
    return r;
}

使用代码:

var sub = GetSubject();
sub.Subscribe(Console.WriteLine);

每次有人订阅 sub 时,他们都会获得迄今为止已在主题中发布的所有值以及新值该主题将在未来生成

I would suggest that the object should not expose a IEnumerable as that is for "cold observable values", where in your case you need something which can get additional items in future also.

The best way to model this would be to use ReplaySubject<T> instead of IEnumerable. Below is an example that demonstrate the situation similar of yours:

//Function to generate the subject with future values
public static ReplaySubject<int> GetSubject()
{
    var r = new ReplaySubject<int>();
    r.OnNext(1); r.OnNext(2); r.OnNext(3);
    //Task to generate future values
    Task.Factory.StartNew(() =>
    {
        while (true)
        {
            Thread.Sleep(3000);
            r.OnNext(DateTime.Now.Second);
        }
    });
    return r;
}

Consuming code:

var sub = GetSubject();
sub.Subscribe(Console.WriteLine);

Every time anyone subscribes to sub they will get all the values that have been published in the subject till now and as well as new values that this subject generates in future

只涨不跌 2024-12-09 16:25:22

您可以使用延迟/重播运算符

You can use Defer/Replay Operator

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文