Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning LiveData from repository

This sample Repository has a method public LiveData<Resource<List<Repo>>> loadRepos(String owner)

https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/repository/RepoRepository.java

My question is why add Android APIs (i.e. android.arch.lifecycle.LiveData) into a data layer?

Isn't it anti-Clean Architecture to mix framework APIs with your data/entities?

Also wouldn't it now require using Robolectric to unit test RepoRepository?

like image 666
liminal Avatar asked Jul 20 '17 14:07

liminal


1 Answers

The purpose of LiveData is to let observers know both when the data is ready and when it changes. When you return a LiveData object from a repository what you are saying is that you will get the data for the caller soon (or eventually) and that you will continue to let the caller know about changes or updates to the data. The implementation of LiveData in the Android Architecture is very simple and is independent of the core Android APIs. The Android Architecture classes are actually add-ons which make Android programming easier and more clean.

Because it is independent of the Android core you do not need Roboelectric to test it. However, you will need to manage your threads for testing because LiveData usually requires the use of a background thread to fetch the data for the observers.

like image 190
Troy Heninger Avatar answered Sep 27 '22 21:09

Troy Heninger