Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In which thread android viewmodel runs?

Tags:

android

I am a beginner in Android data binding.In mvvm Architecture patter when we write code in viewmodel.In which thread those are running in main thread or seperate thread.

like image 388
Ritu Suman Mohanty Avatar asked Sep 13 '19 19:09

Ritu Suman Mohanty


2 Answers

You can check using Thread.currentThread() method

In onCreate Method of Activity put log

Log.d(TAG, Thread.currentThread());

In constructor of Viewmodel put another log

Log.d(TAG, Thread.currentThread());

You will see the same thread Id. Another example is when you try to perform db or network operation in viewmodel without using AsyncTask, Thread or RxJava it throw an Exception that Cannot perform operation on MainThread.

I hope this will help you

like image 184
Rajnish suryavanshi Avatar answered Nov 04 '22 11:11

Rajnish suryavanshi


Viewmodel is Lifecycle aware class which is associated with UI. So the answer is Main UI Thread.

like image 40
Brahem Mohamed Avatar answered Nov 04 '22 10:11

Brahem Mohamed