ReactiveX / rxdart

The Reactive Extensions for Dart
http://reactivex.io
Apache License 2.0
3.37k stars 270 forks source link

What is the correct way to close Publish and Behavior subjects? #398

Closed ZatacaSystems closed 4 years ago

ZatacaSystems commented 4 years ago

I've been struggling with this for some time and I first read at some medium post that the correct way to close the Subjects is to call drain and then call close but I noticed that doing this prevents the subjects from being closed, since drain just hangs waiting for something and never ends.

I then changed the order of the sentences and now I use close before drain (since closing a pipe in real life and then draining it is the way that sounds logic) and then the method close of the subjects works.

Is this the right way?

And also, each single StreamSubscription must be canceled? cause I have some that I never cancel (cause usually this subscriptions are created on the bloc and when I dispose the bloc I dispose the subjects where that subscriptions belong)

Thanks for the help

ZatacaSystems commented 4 years ago

So I decided to go for flutter bloc and bloc, since no one can answer this and those pluggins handle the open and closure of subjects automaticaly

frankpepermans commented 4 years ago

A Bloc is basically a BehaviorSubject behind the scenes, this is not an RxDart specific question really, rather a "best practice for Dart Streams" one.

Please see this answer in stack overflow: https://stackoverflow.com/questions/49879438/dart-do-i-have-to-cancel-stream-subscriptions-and-close-streamsinks

(also, check the docs on drain, you do not need to do drain + close really)