Dart流.asBroadcastStream内存泄漏

编程入门 行业动态 更新时间:2024-10-24 08:31:55
本文介绍了Dart流.asBroadcastStream内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我们的Flutter应用程序中,内存泄漏和流没有关闭.我们将源代码追溯到以下代码:

In our Flutter application we have memory leaks and streams not being closed. We traced the source to code such as:

RxbineLatest(...).asBroadcastStream()

RxDart bineLatest()的结果是单订阅流.添加.asBroadcastStream()可使流方便地用于我们的各种Flutter显示器.但是,当这些显示器关闭时,合并的流仍处于活动状态.

The result of RxDart bineLatest() is a single-subscription stream. Adding .asBroadcastStream() makes the stream conveniently available to our various Flutter displays. However when these displays are closed the streams being combined are still active.

推荐答案

来自.asBroadcastStream()文档:

返回的流将在添加第一个订阅者时订阅该流,并且将保持订阅,直到该流结束或回调取消订阅为止.

The returned stream will subscribe to this stream when its first subscriber is added, and will stay subscribed until this stream ends, or a callback cancels the subscription.

因此,根据设计,流一直存在到被明确取消为止.要在最后一个侦听器取消后取消订阅,请使用:

So by design the stream exists until explicitly cancelled. To cancel the subscription when the last listener cancels use:

RxbineLatest(...).asBroadcastStream( onCancel: (sub) => sub.cancel() )

Stream.asBroadcastStream-容易引起泄漏,这是什么?原理? #26686

更多推荐

Dart流.asBroadcastStream内存泄漏

本文发布于:2023-11-08 15:50:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存   Dart   asBroadcastStream

发布评论

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

>www.elefans.com

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