Beginning RxKotlin · Observables and Subjects in Practice, Part 2 | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7419-beginning-rxkotlin/lessons/12

Hi Macsimus,

I was going through the video tutorial and I saw you passing fragment as a method parameter in SharedViewModel. Is it a good practice to expose view to a viewmodel ?

Thank you for the question, Sagar. Here, in this context we’re just using SharedViewModel to allow the flow of events between the photo chooser and its host activity. It would also have been possible to pass the Observable in as the method parameter instead of the fragment, and perhaps that would have been better. Thanks again for the question!

1 Like

wouldnt the idea of passing the fragment to the sharedViewModel and the frament having an instance of the sharedViewModel be technically considered a circular dependency?

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

I agree. You could however pass the Observable itself and avoid passing UI components to the VM.
fun subscribeSelectedPhotos(photoPickerObservable: Observable<Photo>)

@wwmobileteam Thank you for sharing this - much appreciated!

We’re seeing several problems in this example:

  1. The ViewModel holds a reference to the View, which breaks the architecture (View should only subscribe to ViewModel)
  2. Memory leaks are introduced since every call to actionAdd() creates a new Fragment and subscribes to its observable (which keeps the Fragment alive)