5. 5
LiveData (vanilla)
● Observable data holder
● Lifecycle-aware
○ Auto handling of lifecycle-related stuff
○ Avoids stupid crashes
○ Avoids memory leaks
○ App doesn’t do more work than it
6. 6
LiveData (vanilla)
● Observable data holder
● Lifecycle-aware
○ Auto handling of lifecycle-related stuff
○ Avoids stupid crashes
○ Avoids memory leaks
○ App doesn’t do more work than it
19. 19
StatefulLiveData
⬢ How we built it
⬢ How to use it
⬢ How it solves common scenarios in daily app operation
⬢ Integration in existing code & libraries
⬢ Super powers
20. 20
❏ Get updates upon data changes
❏ Be lifecycle-aware
❏ Support every data type
❏ Know when the data is retrieved successfully and ready to
use
❏ Know when the data is loading, and supply some partial data
❏ Know when an error occurred, and know what the error is
Requirements
StatefulLiveData
21. StatefulData
abstract class StatefulData<T> {
class Success<T>(val data: T) : StatefulData<T>()
class Error<T>(val throwable: Throwable) : StatefulData<T>()
class Loading<T>(val loadingData: Any?) : StatefulData<T>()
}
❏ Get updates upon data changes
❏ Be lifecycle-aware
❏ Support every data type
❏ Know when the data is retrieved successfully and ready to use
❏ Know when the data is loading, and supply some partial data
❏ Know when an error occurred, and know what the error is
22. StatefulData
abstract class StatefulData<T> {
class Success<T>(val data: T) : StatefulData<T>()
class Error<T>(val throwable: Throwable) : StatefulData<T>()
class Loading<T>(val loadingData: Any?) : StatefulData<T>()
}
❏ Get updates upon data changes
❏ Be lifecycle-aware
✓ Support every data type
✓ Know when the data is retrieved successfully and ready to use
✓ Know when the data is loading, and supply some partial data
✓ Know when an error occurred, and know what the error is
25. WeatherDashboardActivity
viewModel.weather.observe(...)
WeatherDashboardViewModel
val weather: LiveData<Weather> = weatherRepo.weather
WeatherRepo
val weather = MutableLiveData<Weather>()
fun fetchNewWeather() {
// do some network stuff…
weather.value = networkResponse.data
}
AppLifecycleObserver
@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onForeground() {
weatherRepo.fetchNewWeather()
}
val weatherError= MutableLiveData<Exception>()
val weatherError= weatherRepo.weatherError
viewModel.weatherError.observe(...)