android-livedata

Android LiveData in androidTest returns null

做~自己de王妃 提交于 2021-01-04 03:23:28
问题 I'm running an androidTest instrumentation test and I have a method that returns LiveData from a DAO object using Room. I'm calling the method like so: val animal = roomDatabase.animalsDao().getAnimal(1) animal.observeForever(mMockObserver) assertNotNull(animal.value) I used Mockito to mock the observer: @Mock private lateinit var mMockObserver = Observer<Animal> This should return an instance of LiveData containing the Animal at id 1, but it's null. It's my understanding that in order for

LocalBroadcastManager has been deprecated. What I should use instead in its place?

丶灬走出姿态 提交于 2020-12-04 19:25:03
问题 I'm working on this project in Android in which an aspect requires a CountdownTimer with a foreground service. A few other answers on Stack Overflow mentioned that LocalBroadcastManager would be suitable for my needs. The documentation in Android Developers, however, mentions that it has been deprecated. Any suggestions on what I should use in its place? The documentation mentioned about using LiveData, but I was wondering if there are any easier alternatives. 回答1: LocalBroadcastManager is

LocalBroadcastManager has been deprecated. What I should use instead in its place?

♀尐吖头ヾ 提交于 2020-12-04 19:07:35
问题 I'm working on this project in Android in which an aspect requires a CountdownTimer with a foreground service. A few other answers on Stack Overflow mentioned that LocalBroadcastManager would be suitable for my needs. The documentation in Android Developers, however, mentions that it has been deprecated. Any suggestions on what I should use in its place? The documentation mentioned about using LiveData, but I was wondering if there are any easier alternatives. 回答1: LocalBroadcastManager is

LocalBroadcastManager has been deprecated. What I should use instead in its place?

最后都变了- 提交于 2020-12-04 19:06:36
问题 I'm working on this project in Android in which an aspect requires a CountdownTimer with a foreground service. A few other answers on Stack Overflow mentioned that LocalBroadcastManager would be suitable for my needs. The documentation in Android Developers, however, mentions that it has been deprecated. Any suggestions on what I should use in its place? The documentation mentioned about using LiveData, but I was wondering if there are any easier alternatives. 回答1: LocalBroadcastManager is

LocalBroadcastManager has been deprecated. What I should use instead in its place?

孤人 提交于 2020-12-04 19:06:07
问题 I'm working on this project in Android in which an aspect requires a CountdownTimer with a foreground service. A few other answers on Stack Overflow mentioned that LocalBroadcastManager would be suitable for my needs. The documentation in Android Developers, however, mentions that it has been deprecated. Any suggestions on what I should use in its place? The documentation mentioned about using LiveData, but I was wondering if there are any easier alternatives. 回答1: LocalBroadcastManager is

MutableLiveData: Cannot invoke setValue on a background thread from Coroutine

孤街醉人 提交于 2020-11-25 08:48:34
问题 I'm trying to trigger an update on LiveData from a coroutine: object AddressList: MutableLiveData<List<Address>>() fun getAddressesLiveData(): LiveData<List<Address>> { AddressList.value = listOf() GlobalScope.launch { AddressList.value = getAddressList() } return AddressList } but I get the following error: IllegalStateException: Cannot invoke setValue on a background thread Is there a way to make it work with coroutines? 回答1: Use liveData.postValue(value) instead of liveData.value = value .

MutableLiveData: Cannot invoke setValue on a background thread from Coroutine

亡梦爱人 提交于 2020-11-25 08:47:36
问题 I'm trying to trigger an update on LiveData from a coroutine: object AddressList: MutableLiveData<List<Address>>() fun getAddressesLiveData(): LiveData<List<Address>> { AddressList.value = listOf() GlobalScope.launch { AddressList.value = getAddressList() } return AddressList } but I get the following error: IllegalStateException: Cannot invoke setValue on a background thread Is there a way to make it work with coroutines? 回答1: Use liveData.postValue(value) instead of liveData.value = value .

MutableLiveData: Cannot invoke setValue on a background thread from Coroutine

耗尽温柔 提交于 2020-11-25 08:44:36
问题 I'm trying to trigger an update on LiveData from a coroutine: object AddressList: MutableLiveData<List<Address>>() fun getAddressesLiveData(): LiveData<List<Address>> { AddressList.value = listOf() GlobalScope.launch { AddressList.value = getAddressList() } return AddressList } but I get the following error: IllegalStateException: Cannot invoke setValue on a background thread Is there a way to make it work with coroutines? 回答1: Use liveData.postValue(value) instead of liveData.value = value .