So, I have ViewModel that looks like this
class MyViewModel<E, S> : ViewModel() {
private val _state = MutableLiveData<S>()
val state: LiveData<S>
get() = _state
abstract fun onEventReceived(event: E)
protected fun pushState(state: S) {
_state.value = state
}
}
And as usual I have observer code in my Activity/ Fragment
viewModel.state.observe(this, Observer {
// Do something here
})
My problem is when I try to emit values like this
pushState(State.A)
pushState(State.B)
pushState(State.C)
In my Activity/Fragment, it only received C.
Anyone has experience with this kind of behaviour? Please kindly share your workaround.
Related
Like in title, I have _token
private val _token = MutableLiveData<String>()
that should update
val userPackages: LiveData<List<Packages>> = Transformations.switchMap(_token) {
packagesLiveData(it)
}
after i use
fun setToken(token: String) {
Log.d(TAG, "setToken: $token")
_token.postValue(token)
}
from this Log.D I know that im getting valid string
i tried
_token.value = token
but nothing changed
I see from this function that im calling inside switchmap
private fun packagesLiveData(string: String): LiveData<List<Packages>> {
Log.d(TAG, "switchMap: $string")
return liveData {
tvRepository
.getUserPackage(string)
.asLiveData()
}
}
that im not getting any change (cos this function not being called at all)
or if i inicialize value of _token to any string then i see it being called twice but with this initialized value and not with the value from _token.value that i confirmed while testing is indeed changing but switchmap can never access
EDIT
It looks like i just didn't observed my liveData but now when i try to do this I'm getting error that i don't have get function for this val
I'm getting my model in my fragment from this
private val tvViewModel: TvViewModel by viewModels()
and this is how my observer looks like
tvViewModel.userPackages.observe(viewLifecycleOwner, {
Log.d(TAG, "setTvObservers: ${it?.get(0)}")
})
Requested ViewModel:
#HiltViewModel
class TvViewModel #Inject constructor(
private val tvRepository: TVRepository
) :
ViewModel() {
companion object {
const val TAG = "TvViewModel"
}
private val _tvPath = MutableLiveData<String>()
private val _token = MutableLiveData<String>()
val tvPath: LiveData<String> = _tvPath
val userPackages: LiveData<List<Packages>> = Transformations.switchMap(_token) {
packagesLiveData(it)
}
fun setTvPath(path: String) {
_tvPath.postValue(path)
}
fun setToken(token: String) {
Log.d(TAG, "setToken: $token")
_token.postValue(token)
}
private fun packagesLiveData(string: String): LiveData<List<Packages>> {
Log.d(TAG, "switchMap: $string")
return liveData(Dispatchers.IO) {
tvRepository
.getUserPackage(string)
.asLiveData()
}
}
}
Are you observing this LiveData, i.e. userPackages somewhere?
Because as mentioned here,
The transformations aren't calculated unless an observer is observing the returned LiveData object.
So make sure you are observing userPackages somewhere, if it still doesn't work do tell, we'll try to find a solution again :)
EDIT:
By looking at the way you were observing the LiveData using viewLifecycleOwner, it seems that you are observing it inside a fragment. So, inside a fragment, you do not get the ViewModel by calling by viewModels(). Instead, you need to use by activityViewModels<TvViewModel>
if it still doesn't work let me know, we'll try to look again :)
LATEST UPDATE
ok so even though this doesn't give any compile-time warnings, but inside your packagesLiveData, the tvRepository.getUserPackage(string).asLiveData() returns a LiveData, and you are using livedata wrapper again around it. What you need to do is something like this:
private fun packagesLiveData(string: String): LiveData<List<Packages>> {
Log.d(TAG, "switchMap: $string")
return tvRepository
.getUserPackage(string)
.asLiveData()
}
In my ViewModel I have two MutableLiveData for the response of my webservice :
val getFindByCategorySuccess: MutableLiveData<List<DigitalService>> by lazy {
MutableLiveData<List<DigitalService>>()
}
val getFindByCategoryError: MutableLiveData<String> by lazy {
MutableLiveData<String>()
}
and this method for the request :
fun requestFindByCategory(categoryId: String){
viewModelScope.launch {
when (val retrofitPost = digitalServicesRemoteRepository.getFindByCategoryRequest(categoryId)) {
is ApiResult.Success -> getFindByCategorySuccess.postValue(retrofitPost.data)
is ApiResult.Error -> getFindByCategoryError.postValue(retrofitPost.exception)
}
}
}
It's working fine using it in my Fragment class :
viewModel.getFindByCategorySuccess.observe(viewLifecycleOwner, { digitalServices ->
logD("I have a good response from the webservice; luanch an other fragment now!")
})
The problem is if I go to an other fragment in my observable (using findNavController().navigate(action)). If I go back to the previous fragment, I go automatically to the nextFragment because the observable is called again.
So I'm looking for solutions...
Maybe clearing all my viewmodel when I go back to my fragment ?
Maybe clearing only getFindByCategorySuccess and getFindByCategoryError ?
Maybe an other solution? I think my architecture is not good. What do you think about it ?
By default, a livedata will emit to its current state (the value that exist on it) for any new observer that subscribes to it.
Answering your question, you might try the operator distincUntilChanged transformation, which, according to the documentation:
Creates a new LiveData object that does not emit a value until the source LiveData value has been changed. The value is considered changed if equals() yields false.
But, this showcases a problem with your snippet, and a bad practice that is common when using livedata, you shouldn't expose mutable live data to your observers. Instead, you should expose a non-mutable version of them.
In your case, in my opinion, your view model should look like the following:
private val getFindByCategorySuccess by lazy {
MutableLiveData<List<DigitalService>>()
}
private val getFindByCategoryError by lazy {
MutableLiveData<String>()
}
val onFindByCategorySuccess: LiveData<List<DigitalService>
get() = getFindByCategorySuccess.distincUntilChanged()
val onFindCategoryError: LiveData<List<String>
get() = getFindByCategoryrRror.distincUntilChanged()
And your observers would subscribe as follows:
ExampleFragment
fun setupObservers() {
viewModel.onFindByCategorySuccess.observe(viewLifecycleOwner) { // Do stuff }
}
I hope it helps
I found a solution to my problem using this class :
class SingleLiveEvent<T> : MutableLiveData<T>() {
private val mPending = AtomicBoolean(false)
override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
super.observe(owner, { t ->
if (mPending.compareAndSet(true, false))
observer.onChanged(t)
})
}
override fun setValue(t: T?) {
mPending.set(true)
super.setValue(t)
}
}
Like this :
var getFindByCategorySuccess: SingleLiveEvent<List<DigitalService>> = SingleLiveEvent()
var getFindByCategoryError: SingleLiveEvent<String> = SingleLiveEvent()
class RequestViewModel(private val repository: RequestRepository) : ViewModel() {
// request currency type
private val currencySearchType = MutableLiveData<String>()
val requests: LiveData<PagedList<Request>> = repository.getRequests(currencySearchType.value!!)
fun updateSearchType(type: String) {
currencySearchType.postValue(type)
}}
above is the code in my viewModel.
private fun initAdapter() {
recyclerView.adapter = adapter
viewModel.requests.observe(viewLifecycleOwner, Observer {
adapter.submitList(it)
})
}
and this is the code in fragment. So basically, what i'm doing is I observe "requests" in viewModel, and in fragment, I will also call updateSearchType to update the currencySearchType. I was hoping that once currencySearchType changed, then the viewmodel.requests will change too. But it turns out the requests never gotta called again. Does anyone know where it went wrong? Appreciate for the help!
Changing the value of currencySearchType does not triggers live data you just using it as function parameter. You have to use Transformation for this.
https://developer.android.com/reference/android/arch/lifecycle/Transformations
class RequestViewModel(private val repository: RequestRepository) : ViewModel() {
// request currency type
lateinit var currencySearchType:String
val requests: LiveData<PagedList<Request>> = Transformations.switchMap(currencySearchType) { repository.getRequests(it) }
fun updateSearchType(type: String) {
currencySearchType = type
}}
I'm working with MVVM, and I have made different implementations of it, but one thing that is still making me doubt is how do I get data from a Repository (Firebase) from my ViewModel without attaching any lifecycle to the ViewModel.
I have implemented observeForever() from the ViewModel, but I don't think that is a good idea because I think I should communicate from my repository to my ViewModel either with a callback or a Transformation.
I leave here an example where I fetch a device from Firebase and update my UI, if we can see here, I'm observing the data coming from the repo from the UI, but from the ViewModel I'm also observing data from the repo, and here is where I really doubt if I'm using the right approach, since I don't know if observeForever() will be cleared on onCleared() if my view is destroyed, so it won't keep the observer alive if the view dies.
UI
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val deviceId = editText.text.toString().trim()
observeData(deviceId)
}
}
fun observeData(deviceId:String){
viewModel.fetchDeviceData(deviceId).observe(this, Observer {
textView.text = "Tipo: ${it.devType}"
})
ViewModel
class MainViewmodel: ViewModel() {
private val repo = Repo()
fun fetchDeviceData(deviceId:String):LiveData<Device>{
val mutableData = MutableLiveData<Device>()
repo.getDeviceData(deviceId).observeForever {
mutableData.value = it
}
return mutableData
}
}
Repository
class Repo {
private val db = FirebaseDatabase.getInstance().reference
fun getDeviceData(deviceId:String):LiveData<Device>{
val mutableData = MutableLiveData<Device>()
db.child(deviceId).child("config/device").addListenerForSingleValueEvent(object: ValueEventListener{
override fun onDataChange(dataSnapshot: DataSnapshot) {
val device = dataSnapshot.getValue(Device::class.java)
mutableData.value = device
}
override fun onCancelled(dataError: DatabaseError) {
Log.e("Error","handle error callback")
}
})
return mutableData
}
}
This example just shows how to fetch the device from Firebase, it works, but from my ViewModel, it keeps making me think that observeForever() is not what I'm looking for to communicate data between the repository to the ViewModel.
I have seen Transformations, but I, in this case, I just need to deliver the entire Device object to my UI, so I don't need to transform the Object I'm retrieving to another Object
What should be here the right approach to communicate the repository and the ViewModel properly?
is observeForever lifecycle aware?
No, that's why it's called observeForever.
I have implemented observeForever() from the ViewModel, but I don't think that is a good idea
No, it's not, you should be using Transformations.switchMap {.
since I don't know if observeForever() will be cleared on onCleared() if my view is destroyed, so it won't keep the observer alive if the view dies.
Well if you're not clearing it in onCleared() using removeObserver(observer), then it won't clear itself, because it observes forever.
here is where I really doubt if I'm using the right approach,
No, you can do much better than this following a reactive approach.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val deviceId = editText.text.toString().trim()
viewModel.onSelectedDeviceChanged(deviceId)
}
viewModel.selectedDevice.observe(this, Observer { device ->
textView.text = "Tipo: ${device.devType}"
})
}
And
class MainViewModel(
private val savedStateHandle: SavedStateHandle,
): ViewModel() {
private val repo = Repo() // TODO: move to Constructor Argument with ViewModelProvider.Factory
private val selectedDeviceId: MutableLiveData<String> = savedStateHandle.getLiveData<String>("selectedDeviceId")
fun onSelectedDeviceChanged(deviceId: String) {
selectedDeviceId.value = deviceId
}
val selectedDevice = Transformations.switchMap(selectedDeviceId) { deviceId ->
repo.getDeviceData(deviceId)
}
}
And
class Repo {
private val db = FirebaseDatabase.getInstance().reference // TODO: move to constructor arg? Probably
fun getDeviceData(deviceId:String) : LiveData<Device> {
return object: MutableLiveData<Device>() {
private val mutableLiveData = this
private var query: Query? = null
private val listener: ValueEventListener = object: ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
val device = dataSnapshot.getValue(Device::class.java)
mutableLiveData.value = device
}
override fun onCancelled(dataError: DatabaseError) {
Log.e("Error","handle error callback")
}
}
override fun onActive() {
query?.removeEventListener(listener)
val query = db.child(deviceId).child("config/device")
this.query = query
query.addValueEventListener(listener)
}
override fun onInactive() {
query?.removeEventListener(listener)
query = null
}
}
}
}
This way, you can observe for changes made in Firebase (and therefore be notified of future changes made to your values) using LiveData, rather than only execute a single fetch and then not be aware of changes made elsewhere to the same data.
To use ObserveForever, you need to remove the observer inside onClear in the ViewModel.
In this case, I would suggest to use Transformation even though you just need a direct mapping without any processing of the data, which is actually the same as what you are doing with the observer for observerForever.
observeForever() is not Lifecycle aware and will continue to run until removeObserver() is called.
In your ViewModel do this instead,
class MainViewmodel: ViewModel() {
private val repo = Repo()
private var deviceData : LiveData<Device>? = null
fun fetchDeviceData(deviceId:String):LiveData<Device>{
deviceData = repo.getDeviceData(deviceId)
return deviceData!!
}
}
I am using MutableLiveData within my application for event based communication. I have single activity two fragments architecture.
With the help of ViewModel, I'm consuming the LiveData events in Fragment-1. But, when I replace this Fragment-1 with Fragment-2 using Menu bar and finally come back to Fragment-1, old values of LiveData are captured again.
How to avoid this problem? Any help/suggestions are highly appreciated!
Thank you.
You can use Event to wrap LiveData values to handle consuming its values as in the following article:
https://medium.com/androiddevelopers/livedata-with-snackbar-navigation-and-other-events-the-singleliveevent-case-ac2622673150
Event class would be like:
open class Event<out T>(private val content: T) {
var hasBeenHandled = false
private set // Allow external read but not write
/**
* Returns the content and prevents its use again.
*/
fun getContentIfNotHandled(): T? {
return if (hasBeenHandled) {
null
} else {
hasBeenHandled = true
content
}
}
/**
* Returns the content, even if it's already been handled.
*/
fun peekContent(): T = content
}
Let us say that your LiveData value is a String then the LiveData of single event would be like:
val navigateToDetails = MutableLiveData<Event<String>>()
Problem with accepted answer is that you can only have one observer.
This article describes solution with multiple observers.
Wherever you're observing the liveData, in onChanged method remove the observers by calling myLiveDataObject.removeObservers(this);
This will remove the observer after first-time data is observed.
I faced the same problem and came up with this library to solve it
https://github.com/ueen/LiveEvent
Hope this helps, enjoy!
Simple, clean, reusable:
class Event<T>(val payload: T, var broadcasted: Boolean = false)
class MutableEventLiveData<T>: MutableLiveData<Event<T>>() {
fun postEvent(value: T) {
super.postValue(Event(value))
}
}
typealias EventLiveData<T> = LiveData<Event<T>>
class EventObserver<T>(private val broadcastCallback: (t: T)->Unit): Observer<Event<T>> {
override fun onChanged(e: Event<T>) {
if (!e.broadcasted) {
broadcastCallback(e.payload)
e.broadcasted = true
}
}
}
Sample usage:
class YourViewModel : ViewModel() {
private val _errorEvent = MutableEventLiveData<String>()
val errorEvent: EventLiveData<String>
get() = _errorEvent
fun fireErrorEvent(errorMessage: String) {
_errorEvent.postEvent(errorMessage)
}
...
}
class YourActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
...
//Note!!! EventObserver handles events not Observer
viewModel.errorEvent.observe(this, EventObserver {
errorMessage -> showErrorMessage(errorMessage)
})
}
...
}