9

Let's say I have an Flowable that is shared among different parts of the application.

In each fragment where I want to observe it, I convert it to a LiveData with LiveDataReactiveStreams.fromPublisher to avoid leaks and crashes. I now have a LiveData that wraps my Flowable.

I then pass the LiveData to my ViewModel (in the ViewModelFactory). As far as I understand, I can go ahead and use the LiveData without worrying about leaks.

Now, instead of observing the LiveData directly, I am tempted to convert it back to a Flowable with LiveDataReactiveStreams.toPublisher and Flowable.fromPublisher and subscribe to the Flowable instead. This is now a Flowable that wraps a LiveData which wraps a Flowable

My question is: Do I have to worry about disposing the subscriptions to this Flowable? My hope is that the LiveData will act as a "barrier", preventing my context to leak back up to the root Flowable, but I am not so sure about that.

In other words:

  1. Flowable A exists in a global context
  2. In each fragment, A is wrapped in LiveData B which is set as a property of the fragments ViewModel
  3. When normally I would observe LiveData B, I instead wrap it in Flowable C
  4. I subscribe to Flowable C and ignore the returned disposable

Will views accessed in C leak up to A when the fragment is destroyed?

1 Answer 1

2
+50

Considering the current implementation, you still need to care for the subscriptions manually. The lifecycle is only used for handling the observation of the live data.

mLiveData.observe(mLifecycle, LiveDataSubscription.this);

The observation is only canceled automatically when a non-positive amount of items was requested and an error is sent. This then disposes the subscription. Since the producer never completes it'll never dispose the subscription by itself and thus you'll leak the subscription if you don't dispose it yourself.

4
  • I understand that it doesn't unsubscribe for me, but my question is: Will it actually leak when the fragment is destroyed? If so, where/how does this leak happen? Dec 26, 2017 at 12:42
  • It will leak your subscription to the publisher.
    – tynn
    Dec 26, 2017 at 12:59
  • I'm with you this far, but won't the publisher be GC:ed (along with the subscription) when the fragment is GC:ed? As far as I can see, all references created here are circular, which should prevent leaks from happening (unless I do something obviously stupid) Dec 27, 2017 at 10:27
  • 2
    If I understand you correctly, you'll possibly never GC the fragment if you inline the side-effects to the stream. If you don't do this, the reference to the publisher or disposable within the fragment should not even be the only one and thus the stream stays active. That's why you have to always dispose of the subscription within your Android lifecycle somewhere.
    – tynn
    Dec 28, 2017 at 6:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.