MVVM - MediatorLiveData onChanged 未被调用

MVVM - MediatorLiveData onChanged not being called

我不确定我在哪里错误地实施了 MediatorLiveData。

我有一个公开可变实时数据对象的存储库 class:

private MutableLiveData<List<UserTransaction>> mTransactionsLiveData
        = new MutableLiveData<>();

public MutableLiveData<List<UserTransaction>> getTransactionsLiveData() {
    return mTransactionsLiveData;
}

我已经将此 MutableLiveData 的引用传递给了我的 ViewModel class getter:

public class UserTransactionsViewModel extends AndroidViewModel {

    private LiveData<List<UserTransaction>> mTransactionsLiveData;
    private SharedRepo                      mRepo;

    public UserTransactionsViewModel(@NonNull Application application) {
        super(application);

        mRepo = new SharedRepo(application);
        mTransactionsLiveData = mRepo.getTransactionsLiveData();
    }

    public LiveData<List<UserTransaction>> getTransactionsLiveData() {
        return mTransactionsLiveData;
    }

    public void getUserTransactions(int userId) {
        mRepo.getUserTransactions(userId);
    }


}

在我的片段 class 中,我正在观察它:

mViewModel.getTransactionsLiveData().observe(getViewLifecycleOwner(), new Observer<List<UserTransaction>>() {
            @Override
            public void onChanged(List<UserTransaction> list) {

                //Observing it here

                loadDataIntoRecycler(list);
            }
        });

我困惑的地方:

我现在正在尝试添加一个 MediatorLiveData 来格式化 ViewModelRepository class.

接收的数据

所以我首先在ViewModel

中添加了一个新的new MediatorLiveData<>()
private MediatorLiveData<List<UserTransaction>> mediatorLiveData
        = new MediatorLiveData<>();

并附上 .addSource


private MediatorLiveData<List<UserTransaction>> mediatorLiveData = new MediatorLiveData<>();

public UserTransactionsViewModel(@NonNull Application application) {
        super(application);

        mRepo = new SharedRepo(application);
        mTransactionsLiveData = mRepo.getTransactionsLiveData();
        
        mediatorLiveData.addSource(mTransactionsLiveData, new Observer<List<UserTransaction>>() {
            @Override
            public void onChanged(List<UserTransaction> userTransactions) {
                 Log.d(TAG, "onChanged: Formatting data...");

                 for(int i = 0; i < list.size(); i++){
                    list.get(i).setTime("Changing time");
                }
            }
        });
    }

我很困惑,我是否让 Fragment 中的观察者听到 MediatorLiveData,而不是 LiveData?因为从未调用 mediatorLiveData 上的 .onChanged 方法。

MediatorLiveData 仅在其处于活动状态时(有一个活跃的观察者)观察其来源,所以是的,您应该在您的 Fragment 中观察它。当没有活跃的观察者时,不会调用onChanged。

  1. 从未调用 mediatorLiveData 上的 .onChanged 方法

  2. 并且,如果您已经尝试从 Fragment 观察 mediatorLiveData 并且 .onChanged 方法仍然没有被调用,则意味着被观察者从未改变。 从未改变表示从不being setValue or postValue.

像这样:

mediatorLiveData.addSource(mTransactionsLiveData,
        new Observer<List<UserTransaction>>() {
            @Override
            public void onChanged(List<UserTransaction> userTransactions) {
                Log.d(TAG, "onChanged: Formatting data...");

                for(int i = 0; i < list.size(); i++){
                    list.get(i).setTime("Changing time");
                }

                mediatorLiveData.setValue(list);
            }
        });

或者,使用 Transformations.map(和 Java 8 lambda)可能更干净(从片段中观察 userTransactions):

public LiveData<List<UserTransaction>> userTransactions;

userTransactions = Transformations.map(mTransactionsLiveData, list -> {
            Log.d(TAG, "onChanged: Formatting data...");

            for(int i = 0; i < list.size(); i++){
                list.get(i).setTime("Changing time");
            }
            return list;
        });