使用反应式扩展来监视 IEnumerable
我正在连接到一个对象,该对象将对象集合异步加载到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议该对象不应该公开 IEnumerable,因为那是“冷可观察值”,在您的情况下,您需要一些可以在将来获得其他项目的东西。
对此进行建模的最佳方法是使用
ReplaySubject
而不是 IEnumerable。下面是一个演示与您类似的情况的示例:使用代码:
每次有人订阅
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:Consuming code:
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您可以使用延迟/重播运算符
You can use Defer/Replay Operator