JakeWharton / RxBinding

RxJava binding APIs for Android's UI widgets.
Apache License 2.0
9.69k stars 971 forks source link

Strong reference to view #366

Closed Thomvis closed 6 years ago

Thomvis commented 7 years ago

Many methods in RxBinding are documented with the following warning:

* <em>Warning:</em> The created observable keeps a strong reference to {@code view}. 
* Unsubscribe to free this reference.

In addition to unsubscribing, I think you should also not have any strong reference to the created observable itself, since its OnSubscribe holds a reference to the view. This was not immediately apparent to me, but I do have to think about it because I regularly store the returned observable in a field (of the view model).

I'd be happy to hear what/if you think should be done:

  1. RxBinding should be updated with an extended warning
  2. I should stop keeping references to the observable
  3. Nothing, because I am wrong
  4. Something else, because...

Thanks!

tadfisher commented 7 years ago

The warning is pretty clear: The created observable keeps a strong reference to {@code view}.

This seems to be the appropriate course of action:

I should stop keeping references to the observable

You should be holding the Disposable returned from a subscription if anything, but there's very little reason to keep a reference to the Observable itself.

danielgomezrico commented 7 years ago

You can use a CompositeSubscription and add every subscription to it, then in onDestroy call clear() on it to release everything. check here https://www.youtube.com/watch?v=QdmkXL7XikQ

JakeWharton commented 6 years ago

What they said. We give you a handle to the strong reference that you can dispose of in your lifecycle when it's no longer needed.