Is this a good approach or I've just found a nasty workaround?

I'm using MediatorLiveData class because seems useful to update the source of a LiveData object.

I mean, the majority of tutorials that I've found on internet just use Livedata or MutableLivedata without a dynamic source, in example:

fun search(/*no input params*/): Call<List<Person>>

But in my case, I have the following web service that performs a search by name:

interface APIServidor {
    fun search(@Query("name") name: String): Call<List<Person>>


public class PeopleRepository {

    public LiveData<List<Person>> search(String name){

        final MutableLiveData<List<Person>> apiResponse = new MutableLiveData<>();
        Call<List<Person>> call = RetrofitService.Companion.getInstance().getApiServer().search(name);
        call.enqueue(new Callback<List<Person>>() {
            public void onResponse(@NonNull Call<List<Person>> call, @NonNull Response<List<Person>> response) {
                if (response.isSuccessful()) {

            public void onFailure(@NonNull Call<List<Person>> call, @NonNull Throwable t) {

        return apiResponse;

Then in the viewmodel class I'm adding source per new request.

public class SearchViewModel extends ViewModel {

    private MediatorLiveData<List<Person>> mApiResponse;
    private PeopleRepository mApiRepo;

    public SearchViewModel() {
        mApiResponse = new MediatorLiveData<>();
        mApiRepo = new PeopleRepository();

    public LiveData<List<Person>> getPlayers() {
        return mApiResponse;

    public void performSearch(String name){
        mApiResponse.addSource(mApiRepo.search(name), new Observer<List<Person>>() {
            public void onChanged(List<Person> apiResponse)            {


bt_search.setOnClickListener {
    val player_name = et_player.text.toString()

Project scope

I'm in a personal project


Use MVVM + Live data + Repository pattern


I've only found tutorials with a simple approach: observe a LiveData object that access to a repository object and fetch data only once.

In example: Fetch all people (select * from people) from web service.

My case: Fetch people that mach a name (select * from people where name=?) from web service.

https://medium.com/@elye.project/kotlin-and-retrofit-2-tutorial-with-working-codes-333a4422a890 https://medium.com/@sriramr083/error-handling-in-retrofit2-in-mvvm-repository-pattern-a9c13c8f3995


Is a good idea use MediatorLiveData class to merge all requests took from user input?

Should I use MutableLiveData and change the repository class and use a custom clousure?

Is there a better approach?

  • 1
    \$\begingroup\$ Does it work for whatever you want it to do? Please add a description of what problem your code solves in less general terms. You're doing something with people and companions, but if you tell us why you're doing what you're doing and what it is (example usage would be great) the extra context would be quite helpful for reviewers. \$\endgroup\$ – Mast Sep 26 '18 at 6:35
  • 1
    \$\begingroup\$ @Mast I've updated the question. If you need more details just let me know. \$\endgroup\$ – Ricardo Sep 26 '18 at 7:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.