First I don't know everything in Android and Java I am working on that, in case you find lack of knowledge in my question.
When I read about the benefits of Loaders, I read that loaders run on separate threads to prevent unresponsive UI.
After appearing and reading of ViewModel and LiveData and Room and after reading this post, especially in the section Observing data which says
In the
Loader
world, getting your data to your UI would involve aLoaderManager
, callinginitLoader()
in the right place, and building a > ?LoaderCallbacks
. The world is a bit more straightforward in the Architecture Components world. etc..
and the next section
Load all the things which says in the second paragraph
For example, Room lets you have observarable queries — database queries that return
LiveData
so that database changes automatically propagate up through yourViewModel
to your UI. Kind of like aCursorLoader
without touching Cursors or Loaders.
All of the above implies that ViewModel and LiveData with Room can work in the background thread, I mean when we use ViewModel and LiveData with Room instead of Loaders, we needn't have used AsyncTask or AsyncTaskLoader or Executer
But in theses examples:
android-room-with-a-view which can be used as a template as they say in README.md,
In the MainActivity.java they write mWordViewModel.getAllWords().observe(this, .... ;
and if we trace getAllWords()
deeply we find that the function is query of select, but when they want to insert data in WordRepository.java
or WordRoomDatabase.java they use AsyncTask,
I mean why have they used ViewModel and LiveData and Dao with query of select, and in the query of insert they have included AsyncTask with ViewModel and Dao?, because as what implies above ViewModel and LiveData with Room can work in the background thread?
My previous question goes for this example android-persistence at step3_solution, I mean to get the data only ViewModel is used but to insert data like in DatabaseInitializer.java, AsyncTask is included?
Also in this example of BasicSample, Executer is included when data has to be inserted?
Thank you very much