首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >安卓观察多个MutableLiveData

安卓观察多个MutableLiveData
EN

Stack Overflow用户
提问于 2020-02-27 04:46:02
回答 1查看 1.1K关注 0票数 0

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

我的源代码片段

代码语言:javascript
复制
viewModel.setPickupLocation(pickupLatLng);
viewModel.setDestinationLatLng(destinationLatLng());

我的地图片段

代码语言:javascript
复制
@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);
    }
});

我的ViewModel

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2020-02-27 08:25:22

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

然后创建一个helper方法

代码语言:javascript
复制
private void setupData(){
    if(variable1 == null || variable2 == null || variable3 == null){
        return;
    }
    plotRoute(xxxxxx);
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60422278

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档