I am doing my first internship and I was converting RX codes to LiveData in a project. At some point, I had to replace subscribe() functions with observeForever() + Globalscope(Dispatchers.Main) in some repositories but apparently using observeForever() is not the best thing to do and my internship mentor suggested me to use Transformations.map() instead .
I am not sure about how to use map() instead of observeForever in the following code (In a repository) :
//I am using Globalscope with Dispathers.Main otherwise I get "cannot observeForever in a background Thread error
GlobalScope.launch(Dispatchers.Main){
someBooleanLiveData.observeForever {
if (it) {
// DO SOMETHING
} else {
// DO SOMETHING ELSE
}
}
}
What I understand from Transformations.map() function is that, it is used to map the value of a given LiveData object, just like the Map operator of ReactiveX
I tried this but doesn't seem to do what I want :
Transformations.map(someBooleanLiveData){
if (it) {
// DO SOMETHING
} else {
// DO SOMETHING ELSE
}
}
My question is how should I use Transformation.map() to avoid observeForever ?
DO SOMETHING : may be a livedataObject.postValue(it) if you need an example
Thanks in advance for replies.