JakeWharton / RxBinding

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

Question about using RxBinding + VM #503

Open desgraci opened 5 years ago

desgraci commented 5 years ago

It is a good idea to have a RxBinding Observable passed to my VM, apply the business logic, then add to the VM disposable bag, and subscribing to that Observable on the Activity/Fragment?

Just wondering, since I have some Business logic on the VM for processing the input, but in theory I'm sending a reference of the view to the VM, which sounds, not that great, also there is a weird going back and forth that I don't personally like, how you guys think it would be best handled this cases?

alexsullivan114 commented 5 years ago

If you pass an RxBInding observable into an android ViewModel and only dispose in the ViewModels onCleared method you'll end up leaking the activity, since the observable keeps a strong reference to the activity.

Here's a sample repository that uses LeakCanary to listen for leaks. If you run the app and rotate the screen and wait a few moments you'll see LeakCanary make some noise about a leaked activity.

From what I can tell the best option is to create a PublishSubject and subscribe to the observable produced via RxBInding in your Activity/Fragment and forward the passed through data to that PublishSubject. You can then dispose of the observable created in your Activity/Fragment.

This isn't a particularly satisfactory approach so I'm very open to any other suggestions.