android-livedata

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 to get the first or (any) element from a LiveData List in Android MVVM architecture?

北慕城南 提交于 2020-07-05 12:27:09
问题 I used MVVM architecture to build an Android application with a Repository as a mediation between the ViewModel and Room database. In one of the functions, I retrieve a list of objects from a table in the repository class so that I can call it from the ViewModel class. how to retrieve the first element in the list from the repository class instead of observing the LiveData in any one of the activities? here is my repository class: public class StudentRepository { private StudentDAO

how to get the first or (any) element from a LiveData List in Android MVVM architecture?

淺唱寂寞╮ 提交于 2020-07-05 12:27:07
问题 I used MVVM architecture to build an Android application with a Repository as a mediation between the ViewModel and Room database. In one of the functions, I retrieve a list of objects from a table in the repository class so that I can call it from the ViewModel class. how to retrieve the first element in the list from the repository class instead of observing the LiveData in any one of the activities? here is my repository class: public class StudentRepository { private StudentDAO

Paging library - Boundary callback for network + db with API taking page and size

感情迁移 提交于 2020-07-04 07:23:01
问题 Short question : What is the correct way to handle database + network on the Paging library from Architecture components, using an API that uses page + size to load a new page and the BoundaryCallback class? Research and explanation Currently the class BoundaryCallback used in the paging library for architecture components, receives as parameter the instance of an element in the list without actual context of where this element is at. It happens in onItemAtFrontLoaded and onItemAtEndLoaded .

Paging library - Boundary callback for network + db with API taking page and size

心不动则不痛 提交于 2020-07-04 07:21:33
问题 Short question : What is the correct way to handle database + network on the Paging library from Architecture components, using an API that uses page + size to load a new page and the BoundaryCallback class? Research and explanation Currently the class BoundaryCallback used in the paging library for architecture components, receives as parameter the instance of an element in the list without actual context of where this element is at. It happens in onItemAtFrontLoaded and onItemAtEndLoaded .

How to observe the livedata with multiple observe for correct way in Android?

杀马特。学长 韩版系。学妹 提交于 2020-07-03 07:27:46
问题 I have an Activity, and it will turn to the fragment-A and then turn to the fragment-B like the following. Activity -> Fragment-A -> Fragment-B situation 1 These two fragment observe the same LiveData for showing snackBar like the following . viewModel.responseData.observe(this, Observer { it.getContentIfNotHandled()?.let { showSnackBar(it) } The livedata in viewModel: var responseData = MutableLiveData<Event<String>>() responseData.value = Event("$message") Error: When I use the above code.

Is it a good practice to observeForever in Repository class? db+network paged list

泪湿孤枕 提交于 2020-06-26 03:23:31
问题 Im building an app following architecture guidelines.Implemented room db caching + network.Need to get latest page number from separate entity. My model: @Entity(tableName = "top_rated_movie_page") public class Top_Rated_Movies_Page { @PrimaryKey(autoGenerate = true) private int db_id; private Integer page; private Integer total_results; private Integer total_pages; @Ignore private List<Result> results; ... Result class contains data which i display in my paged list which observes changes

Coroutines - unit testing viewModelScope.launch methods

痴心易碎 提交于 2020-06-24 08:27:45
问题 I am writing unit tests for my viewModel, but having trouble executing the tests. The runBlocking { ... } block doesn't actually wait for the code inside to finish, which is surprising to me. The test fails because result is null . Why doesn't runBlocking { ... } run the launch block inside the ViewModel in blocking fashion? I know if I convert it to a async method that returns a Deferred object, then I can get the object by calling await() , or I can return a Job and call join() . But , I'd

LiveData not able to observe the changes

心不动则不痛 提交于 2020-06-17 04:15:46
问题 The bounty expires in 5 days . Answers to this question are eligible for a +50 reputation bounty. kgandroid wants to draw more attention to this question. I am updating a LiveData value from a DialogFragment in the ViewModel , but not able to get the value in Fragment. The ViewModel: class OtpViewModel(private val otpUseCase: OtpUseCase, analyticsModel: IAnalyticsModel) : BaseViewModel(analyticsModel) { override val globalNavModel = GlobalNavModel(titleId = R.string.otp_contact_title,

RefreshShows() in Repository get called every time onCreate() callback of Activity get called

主宰稳场 提交于 2020-06-01 06:24:22
问题 I have following project in Github : https://github.com/Ali-Rezaei/TVMaze I have started to using Koin as dependency injection framework in a sample app : class TVMazeApplication : Application() { override fun onCreate() { super.onCreate() startKoin { androidContext(this@TVMazeApplication) modules(networkModule) modules(persistenceModule) modules(repositoryModule) modules(viewModelModule) } } } This is my repository class : class ShowRepository( private val dao: ShowDao, private val api: