Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] mvvm – Android Observing Multiple MutableLiveData

Posted on November 11, 2022 By

Solution 1 :

Have 3 variables in MyMapFragment keeping track of each view model request. Make all 3 variables null at fragment creation. When each viewModel request completes, change the corresponding variable.

Then make a helper method

private void setupData(){
    if(variable1 == null || variable2 == null || variable3 == null){
        return;
    }
    plotRoute(xxxxxx);
}

Then call setupData() in each of your viewmodel responses in place of the triple nested if statement checks

Problem :

This question already has answers here:

Best practice for using MediatorLiveData only by present data

(2 answers)
Closed 2 years ago.

I have an MVVM setup, with two fragments interacting via a ViewModel. My SourceFragment is updating two MutableLiveData LatLngs in the VM. My second is a MapFragment in which I am drawing a route with the two LatLng LiveData. Both fragments are loaded as the Activity starts up. The SourceFragment updates the LatLngs. I need to wait for the map to load before requesting the route being plotted and I have a third MutableLiveData Boolean that I update once the map is loaded. Currently the way I achieve this is by setting up three separate viewModel Observers for the three MutableLiveData in my MapFragment with a set of if statements to check when all three MutableLiveData are set. I have a feeling that this could be done way better with something like MediatorLiveData but I have not managed to find an example I can work off. Is MediatorLiveData or something else applicable here?

My Source Fragment

viewModel.setPickupLocation(pickupLatLng);
viewModel.setDestinationLatLng(destinationLatLng());

My Map Fragment

@Override
public void onActivityCreated(@NonNull Bundle savedInstanceState) {
    Log.d(TAG, "onActivityCreated: called");
    super.onActivityCreated(savedInstanceState);

    getCustomerOrDriverViewModel();

}

private void initViewModels() { 

    viewModel.getPickupLocation().observe(getViewLifecycleOwner(), new Observer<LatLng>() {
        @Override
        public void onChanged(@NonNull LatLng pickupLocation) {
            Log.d(TAG, "onChanged: from onActivityCreated: called for getPickupLocation");

            if(viewModel.getMapReady().getValue() != null) {
                if(viewModel.getMapReady().getValue()) {
                    resetRoute();

                    setSingleMarker(pickupLocation);
                    if(viewModel.getDestinationLatLng().getValue() != null) {
                        plotRoute(viewModel.getDestinationLatLng().getValue(), pickupLocation);
                    }
                }
            }
        }
    });

    viewModel.getDestinationLatLng().observe(getViewLifecycleOwner(), new Observer<LatLng>() {
        @Override
        public void onChanged(@NonNull LatLng destinationLatLng) {
            Log.d(TAG, "onChanged: from onActivityCreated: called for getPickupLocation");
            if(viewModel.getMapReady().getValue() != null) {
                if(viewModel.getMapReady().getValue()) {
                    resetRoute();

                    if(viewModel.getPickupLocation().getValue() != null) {
                        plotRoute(destinationLatLng, viewModel.getPickupLocation().getValue());
                    }
                }
            }
        }
    });

    viewModel.getMapReady().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
        @Override
        public void onChanged(@NonNull Boolean ready) {
            Log.d(TAG, "onChanged: from onActivityCreated: called for getMapReady");
            if(ready) {
                if(viewModel.getPickupLocation().getValue() != null && viewModel.getDestinationLatLng().getValue() != null) {
                    Log.d(TAG, "onChanged: from onActivityCreated: called for getMapReady: plotting route");
                    resetRoute();
                    plotRoute(viewModel.getDestinationLatLng().getValue(), viewModel.getPickupLocation().getValue());
                }
            }
        }
    });
}

mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
    @Override
    public void onMapLoaded() {
        viewModel.setMapReady(true);
    }
});

My ViewModel

private MutableLiveData<LatLng> pickupLocation = new MutableLiveData<>();
private MutableLiveData<LatLng> destinationLatLng = new MutableLiveData<>();
private MutableLiveData<Boolean> mapReady = new MutableLiveData<>();

public void setPickupLocation(LatLng input) {
    Log.d(TAG, "setPickupLocation: ");
    pickupLocation.setValue(input);
}

public void setDestinationLatLng(LatLng input) {
    Log.d(TAG, "setPickupLocation: ");
    destinationLatLng.setValue(input);
}

public void setMapReady(Boolean input) {
    Log.d(TAG, "setPickupLocation: ");
    mapReady.setValue(input);
}

public MutableLiveData<LatLng> getPickupLocation() {
    Log.d(TAG, "getPickupLocation: ");
    return pickupLocation;
}

public MutableLiveData<LatLng> getDestinationLatLng() {
    Log.d(TAG, "getDestinationLatLng: ");
    return destinationLatLng;
}

public MutableLiveData<Boolean> getMapReady() {
    Log.d(TAG, "getMapReady: ");
    return mapReady;
}
READ  [FIXED] android - I can't send data to sqlite to database
Powered by Inline Related Posts
Android Tags:android, android-viewmodel, mediatorlivedata, mutablelivedata, mvvm

Post navigation

Previous Post: [FIXED] java – How to solve debug issue
Next Post: [FIXED] android – How to solve setting up this payment problem – paypal

Related Posts

[FIXED] c# – Xamarin Android how to show list if user start typing with @ special character like whatsapp tagging features? Android
[FIXED] How can I add items to a list, and therefore delete them, using RecyclerView and a button in Android Studio? Android
[FIXED] flutter – Can’t find Open for Editing in Android Studio Android
[FIXED] How can I move my python code and assets from a windows Laptop to an android phone? Android
[FIXED] how can we build android ndk from android code base Android
[FIXED] android – flutter More than one file was found with OS independent path ‘AndroidManifest.xml’ Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is the rising action in Julius Caesar?
  • How do you secure a rope to itself?
  • Does waterproof laminate scratch easily?
  • What makes a building prewar?
  • What can you learn in a month without alcohol?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme