【问题标题】:Android Observing Multiple MutableLiveData [duplicate]Android观察多个MutableLiveData [重复]
【发布时间】:2020-02-26 20:46:02
【问题描述】:

我有一个 MVVM 设置,两个片段通过 ViewModel 交互。我的 SourceFragment 正在更新 VM 中的两个 MutableLiveData LatLngs。我的第二个是MapFragment,我在其中使用两个 LatLng LiveData 绘制路线。两个片段在 Activity 启动时被加载。 SourceFragment 更新 LatLngs。在请求绘制路线之前,我需要等待地图加载,并且我有第三个 MutableLiveData 布尔值,一旦加载地图,我就会更新它。目前我实现这一点的方法是在我的MapFragment 中为三个MutableLiveData 设置三个单独的viewModel Observers,并使用一组if 语句来检查何时设置了所有三个MutableLiveData。我有一种感觉,使用MediatorLiveData 之类的东西可以做得更好,但我还没有找到一个可以解决的例子。 MediatorLiveData 或其他适用于此吗?

我的源代码片段

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

我的地图片段

@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);
    }
});

我的视图模型

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;
}

【问题讨论】:

    标签: android mvvm android-viewmodel mutablelivedata mediatorlivedata


    【解决方案1】:

    在 MyMapFragment 中有 3 个变量来跟踪每个视图模型请求。在片段创建时使所有 3 个变量为空。当每个 viewModel 请求完成时,更改相应的变量。

    然后做一个辅助方法

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

    然后在每个视图模型响应中调用 setupData() 代替三重嵌套 if 语句检查

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      相关资源
      最近更新 更多