拍摄 ReplaySubject<T> 的快照缓冲

编程入门 行业动态 更新时间:2024-10-28 05:20:54
本文介绍了拍摄 ReplaySubject<T> 的快照缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个很大的 ReplaySubject,它允许订阅者像往常一样接收重播缓冲区和未来的通知.另外,我希望能够拍摄快照"当前缓冲区的数据并将其作为列表同步返回,无需订阅.

I have a large ReplaySubject that allows subscribers to receive the replay buffer and future notifications as normal. In addition, I would like to be able to take a "snapshot" of the current buffer and return that as a list synchronously, without having to subscribe.

有没有办法做到这一点?

Is there a way to do this?

谢谢

推荐答案

你能不能不只是订阅,接收项目,然后取消订阅?

Can you not just subscribe, receive the items, then unsubscribe?

public static List<T> Snapshot<T>(ReplaySubject<T> subject)
{
    List<T> snapshot = new List<T>();
    using (subject.Subscribe(item => snapshot.Add(item))
    {
        // Deliberately empty; subscribing will add everything to the list.
    }
    return snapshot;
}

当然,这是假设订阅 ReplaySubject 会同步调用元素处理程序.你想检查一下,但这正是我所期望的.

That's assuming that subscribing to a ReplaySubject<T> invokes the element handler synchronously, of course. You'd want to check that, but that's what I'd expect.

您还应该考虑是否要以某种方式处理错误/完成.

You should also consider whether you want to handle errors / completion somehow.

这篇关于拍摄 ReplaySubject&lt;T&gt; 的快照缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-05-01 12:28:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409763.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快照   amp   ReplaySubject   gt   lt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!