Search code examples
androidwebrtcandroid-architecture-componentsandroid-viewmodel

Which thread (UI/Background) does view model class of architecture components lives?


I built a webrtc based calling app. All codes are in Activity as of now so as expected it doesn't work well on configuration changes. I'm thinking to put all WebRTC code to view model and update UI from view model. Do I've to do background tasks in separate thread in view model too like I did in activity. Currently I'm using java Thread in activity.


Solution

  • The ViewModel is just a class which instantiates along with an Activity/Fragment. Going through threads is another case and has nothing to do with Viewmodel. Don't forget that if a class extends the ViewModel will have its' own lifecycle.

    Do I've to do background tasks in separate thread in view model too like I did in activity?

    So the answer is YES. Activity, Fragment, ViewModel, Presenter, Interactor or whatever are just classes. You will jump to threads if performing asynchronous or heavy operations wherever you are. Even if we are not talking Android at all