Filling an adapter from different LiveData sources

断了今生、忘了曾经 提交于 2020-01-04 06:25:30

问题


I'm playing with LiveData and want to understand what it can do. I want to fill my RecyclerView with data from different sources by switch(using filters if you like).

Filtering values inside an adapter is not an option. So, I decided to use MediatorLiveData inside my view model.

Dao:

@Query("SELECT * FROM tasks WHERE completed = 0")
LiveData<List<Task>> getActiveTasksLiveData();

@Query("SELECT * FROM tasks")
LiveData<List<Task>> getAllTasksLiveData();

@Query("SELECT * FROM tasks WHERE completed = 1")
LiveData<List<Task>> getClosedTasksLiveData();

Repo:

    public LiveData<List<Task>> getActiveTasks() {
        return mTaskDao.getActiveTasksLiveData();
    }

    public LiveData<List<Task>> getAllTasks() {
        return mTaskDao.getAllTasksLiveData();
    }

    public LiveData<List<Task>> getClosedTasks() {
        return mTaskDao.getClosedTasksLiveData();
    }

ViewModel

public class MainViewModel extends AndroidViewModel {

    private final String TAG = "MainViewModel";

    private final AppDataRepository mData;

    private MediatorLiveData<List<Task>> mMediatorTasks;

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

        mData = AppDataInjector.getDataRepository(application.getApplicationContext());

        mMediatorTasks = new MediatorLiveData<>();
        mMediatorTasks.setValue(null);
    }

    public LiveData<List<Task>> getTasks(){
        return mMediatorTasks;
    }

    public void changeTasksOption(int index){
        mMediatorTasks.removeSource(mData.getAllTasks());
        mMediatorTasks.removeSource(mData.getActiveTasks());
        mMediatorTasks.removeSource(mData.getClosedTasks());
        if (index == R.id.navigation_all){
            Log.i(TAG, "Add source: all");
            mMediatorTasks.addSource(mData.getAllTasks(), new Observer<List<Task>>() {
                @Override
                public void onChanged(List<Task> tasks) {
                    Log.i(TAG, "Add source: all - setValue");
                    mMediatorTasks.setValue(tasks);
                }
            });
        } else if (index == R.id.navigation_closed){
            Log.i(TAG, "Add source closed");
            mMediatorTasks.addSource(mData.getClosedTasks(), new Observer<List<Task>>() {
                @Override
                public void onChanged(List<Task> tasks) {
                    Log.i(TAG, "Add source: closed - setValue");
                    mMediatorTasks.setValue(tasks);
                }
            });
        } else {
            Log.i(TAG, "Add source active");
            mMediatorTasks.addSource(mData.getActiveTasks(), new Observer<List<Task>>() {
                @Override
                public void onChanged(List<Task> tasks) {
                    Log.i(TAG, "Add source: active - setValue");

                    mMediatorTasks.setValue(tasks);
                }
            });
        }
    }
}

Fragment

    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        mNavigationView = view.findViewById(R.id.navigation);
        mFab = view.findViewById(R.id.fabMain);
        mRecyclerView = view.findViewById(R.id.mainRecyclerView);

        tasksAdapterLive = new TasksAdapterLive(mAdapterCallback);
        RecyclerView.LayoutManager manager = new GridLayoutManager(getContext(), 1);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(tasksAdapterLive);

        // set up bottom navigation listener
        mNavigationView.setOnNavigationItemSelectedListener(item -> {
            mViewModel.changeTasksOption(item.getItemId());
            return true;
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

        mViewModel.getTasks().observe(this, tasks -> {
            if (tasks != null) {
                tasksAdapterLive.setTasks(tasks);
                tasksAdapterLive.notifyDataSetChanged();
            }
        });

        mViewModel.changeTasksOption(mNavigationView.getSelectedItemId());
    }

As you can see, I've decided to use MediatorLiveData inside my view model. My main goal - change data inside adapter when changeTasksOption() called from fragment.

I use removeSource(), because how I understand it removes LiveData source from observing. But, in my case it does not.

When I launch app, logs are:

MainViewModel: Add source active
MainViewModel: Add source: active - setValue

When I try switch to another source - logs are

MainViewModel: Add source: all
MainViewModel: Add source: all - setValue
MainViewModel: Add source: active - setValue
MainViewModel: Add source: all - setValue
MainViewModel: Add source: active - setValue
*** repeats about 100 times

RecyclerView is blinking

So, I kindly ask. What am I doing wrong? Did I misunderstood the documentation? What really removeSourse() does? Because in my case it does not remove sources.

In case my method implementing this is wrong, how do you suggest I do?

Thank you!

EDTITED:

After experimenting for couple of hours I've found solution. Yeep, this is bad(or maybe not?). But clearly this is not universal, because we do not use Romm + LiveData

Create normal Room functions that return List

@Query("SELECT * FROM tasks WHERE completed = 0")
List<Task> getActiveTasks();

@Query("SELECT * FROM tasks")
List<Task> getAllTasks();

@Query("SELECT * FROM tasks WHERE completed = 1")
List<Task> getClosedTasks();

Created MutableLiveData in repo

private MutableLiveData<List<Task>> mTasksTestActive, mTasksTestAll, mTasksTestClosed;

Add theese functions to repo

public LiveData<List<Task>> getActiveTasksTest() {
    Executors.newSingleThreadExecutor().execute(() -> {
        List<Task> taskList = mTaskDao.getActiveTasks();
        mTasksTestActive.postValue(taskList);
    });
    return mTasksTestActive;
}

public LiveData<List<Task>> getAllTasksTest() {
    Executors.newSingleThreadExecutor().execute(() -> {
        List<Task> taskList = mTaskDao.getAllTasks();
        mTasksTestAll.postValue(taskList);
    });
    return mTasksTestAll;
}

public LiveData<List<Task>> getClosedTasksTest() {
    Executors.newSingleThreadExecutor().execute(() -> {
        List<Task> taskList = mTaskDao.getClosedTasks();
        mTasksTestClosed.postValue(taskList);
    });
    return mTasksTestClosed;
}

ViewModel changes:

public void changeTasksOption(int index) {
    mMediatorTasks.removeSource(mData.getAllTasksTest());
    mMediatorTasks.removeSource(mData.getActiveTasksTest());
    mMediatorTasks.removeSource(mData.getClosedTasksTest());
    if (index == R.id.navigation_all) {
        Log.i(TAG, "Add source: all");
        mMediatorTasks.addSource(mData.getAllTasksTest(), tasks -> {
            Log.i(TAG, "Add source: all - postValue");
            mMediatorTasks.postValue(tasks);
        });
    } else if (index == R.id.navigation_closed) {
        Log.i(TAG, "Add source closed");
        mMediatorTasks.addSource(mData.getClosedTasksTest(), tasks -> {
            Log.i(TAG, "Add source: closed - postValue");
            mMediatorTasks.postValue(tasks);
        });
    } else {
        Log.i(TAG, "Add source active");
        mMediatorTasks.addSource(mData.getActiveTasksTest(), tasks -> {
            Log.i(TAG, "Add source: active - postValue");

            mMediatorTasks.postValue(tasks);
        });
    }
}

And now, by switching UI, I have my result. No more loops and everything seems go ok.

But still! This is a bad solution. Maybe something is wrong with Room?


回答1:


public void changeTasksOption(int index){
    mMediatorTasks.removeSource(mData.getAllTasks());
    mMediatorTasks.removeSource(mData.getActiveTasks());
    mMediatorTasks.removeSource(mData.getClosedTasks());

No this is not how it should be!

The selected option should be in a LiveData. Then you can use Transformations.switchMap { against that LiveData to select the correct LiveData<List<Task>>.

private MutableLiveData<Integer> mSelectedIndex = new MutableLiveData<>();

private final LiveData<List<Task>> mMediatorTasks = Transformations.switchMap(mSelectedIndex, (index) -> {
    if (index == R.id.navigation_all) {
        return mData.getAllTasksTest();
    } else if (index == R.id.navigation_closed) {
        return mData.getClosedTasksTest();
    } else {
        return mData.getActiveTasksTest();
    }
});

public void changeTasksOption(int index) {
    mSelectedIndex.setValue(index);
}

public LiveData<List<Task>> getTasks(){
    return mMediatorTasks;
}

Also, you should bring your mData.get*() methods to return LiveData<List<Task>> from the DAO again, that was a better solution.




回答2:


You are returning values from your repo synchronously in your previous repo code -

public LiveData<List<Task>> getActiveTasks() {
    return mTaskDao.getActiveTasksLiveData();
}

public LiveData<List<Task>> getAllTasks() {
    return mTaskDao.getAllTasksLiveData();
}

public LiveData<List<Task>> getClosedTasks() {
    return mTaskDao.getClosedTasksLiveData();
}

So when you call removeSource(mData.getAllTasksTest()), it synchronously fetches data from the repo and that is why you are receiving data from all the repos.

In your edited code, you are using a worker thread to fetch data, which means that your source livedata gets removed from the mediator live data before the repo returns any value.



来源:https://stackoverflow.com/questions/54403479/filling-an-adapter-from-different-livedata-sources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!