android-viewmodel

How to access room db using viewmodel, repository, and livedata

孤街浪徒 提交于 2020-08-26 09:46:12
问题 I am new to android development. In my app, I need to have a user table in the database. I have created the database using room. Now, if I allow to run the db in the main thread, my app is running fine. But, I don't want to run it in the main thread. So, I created the repository and viewmodel classes and calling the db in the main thread using the viewmodel. Still if I don't put allowMainThreadQueries() in the database building statement, java.lang.IllegalStateException: Cannot access

Dagger-Hilt: @ViewModelInject is not injecting MyViewModel and crash?

倾然丶 夕夏残阳落幕 提交于 2020-08-07 05:36:08
问题 In exploring the ViewModelInject of Dagger-Hilt, I follow the example in https://developer.android.com/training/dependency-injection/hilt-jetpack#viewmodels I try to inject the ViewModel into my activity as follow import android.app.Application import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.viewModels import androidx.hilt.Assisted import androidx.hilt.lifecycle.ViewModelInject import androidx.lifecycle.* import androidx.savedstate

ViewModelProviders is not working inside my Fragment

◇◆丶佛笑我妖孽 提交于 2020-08-06 12:47:07
问题 This is what I'm trying to do. Set an ArrayList of object inside a Fragment Get that array from an observer within the FragmentActivity container (the activity that hosts all the fragments) So, What I have done is the following. First I created the SharedViewModel from where I will set and get the data from : SharedViewModel.kt class SharedViewModel: ViewModel() { var personArrayObj:MutableLiveData<ArrayList<Person>> = MutableLiveData() fun setPersonArray(personArray:ArrayList<Person>){

JUnit5 test with LiveData doesn't execute subscriber's callback

Deadly 提交于 2020-07-06 17:48:34
问题 Background: I have a simple application that fetches movie list using rests API call. The project structure is given below, Activity -> ViewModel -> Repository -> ApiService (Retrofit Interface) The activity subscribes to a LiveData and listens for events changes The ViewModel hosts the MediatorLiveData observed by the activity. Initially the ViewModel sets a Resource.loading(..) value in MediatorLiveData . The ViewModel then calls the repository to get the movie list from ApiService The

JUnit5 test with LiveData doesn't execute subscriber's callback

好久不见. 提交于 2020-07-06 17:44:37
问题 Background: I have a simple application that fetches movie list using rests API call. The project structure is given below, Activity -> ViewModel -> Repository -> ApiService (Retrofit Interface) The activity subscribes to a LiveData and listens for events changes The ViewModel hosts the MediatorLiveData observed by the activity. Initially the ViewModel sets a Resource.loading(..) value in MediatorLiveData . The ViewModel then calls the repository to get the movie list from ApiService The

JUnit5 test with LiveData doesn't execute subscriber's callback

可紊 提交于 2020-07-06 17:43:05
问题 Background: I have a simple application that fetches movie list using rests API call. The project structure is given below, Activity -> ViewModel -> Repository -> ApiService (Retrofit Interface) The activity subscribes to a LiveData and listens for events changes The ViewModel hosts the MediatorLiveData observed by the activity. Initially the ViewModel sets a Resource.loading(..) value in MediatorLiveData . The ViewModel then calls the repository to get the movie list from ApiService The

How i can add view model in module?

时光怂恿深爱的人放手 提交于 2020-06-29 05:26:15
问题 In Dagger, how can I add a model to Module? For example I added the presenter in the following way: @Module class AboutModule(val appContext: Context) { @FragmentScope @Provides fun providePresenter(): AboutListContract.Presenter { return AboutListPresenter(appContext = appContext) } } Now i want want to add my View model, also with appContext. class AboutViewModel(val appContext: Context): ViewModel() { UPDATE: Can i add my view model smth like this? @Module class AboutModule(val appContext: