Reputation: 13648
So there is a new builder function for LiveData
which is:
val someLiveData = liveData {
// do something
}
Can anyone explain exactly what this new builder function solve? How does it solve issues on rotation? How does it relate to webservice calls?
Any inputs would be appreciated. Thanks in advance.
Upvotes: 5
Views: 675
Reputation: 4902
Can anyone explain exactly what this new builder function solve?
The current documentation on liveData { }
it is pretty good and gives many examples. Here are some benefits you get for free by using it:
timeoutInMs
(which defaults to 5 seconds).init { }
block to initialize a MutableLiveData<T>
(this hypothetical coroutine is referred to as C
below).C
inC
until it is actually needed (i.e. the LiveData
has any registered and active observers).C
when the LiveData is re-activated.How does it solve issues on rotation?
LiveData
in itself does not solve any issue with preserving state across e.g. screen rotation. That's what ViewModel
is for. Typically you have LiveData
properties in your ViewModel
. But there is not direct relationship between screen rotation problems and liveData { }
How does it relate to webservice calls?
Since the block you pass to liveData { }
is a suspend function, you can use coroutine support in your webservice. For example, Retrofit 2.6.0 and later supports suspend
modifiers in its HTTP request function definitions, which makes it very convenient to use in the liveData { }
code block.
Upvotes: 2