Group Group Group Group Group Group Group Group Group

Incorrect use of viewmodels in Reactive Programming with Kotlin book

Hi, I noticed when reading this book, that the author has made a grave error in his/her use of Viewmodels. In chapter 4, Observables & Subjects in practice, the author has a method saveBitmapFromImageView(imageView: ImageView,context: Context): Observable<String> { }, takes a context param. This is explicitly highlighted as wrong in the Android documents… A ViewModel must never reference a view, or any class that may hold a reference to the activity context. If the [ ViewModel ] needs the Application context, for example to find a system service, it can extend the [ AndroidViewModel] class and have a constructor that receives the Application in the constructor, since Application class extends Context. I feel like this needed highlighted and the text/code should be updated

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

@rokerdawg Great catch! You’re absolutely right, the linked code should be using the Application context, either in passed in via the activity or by using the AndroidViewModel. The code will be updated!