简体   繁体   中英

LiveData, Transformations.map() - How to force to refresh when the user changes filtering order?

I use LiveData + Transformations.map() :

private final LiveData<List<Task>> observableTasks;
(...)
observableTasks =  Transformations.map(tasksRepository.getTasks(), tasks-> filterTasks(tasks));

How to force LiveData to refresh? I need Transformations.map() to prepare new list. When the user changes filtering options I need to call filterTasks(tasks) again and show new, sorted list. Data coming from the repository ( tasksRepository.getTasks() ) stays the same.

I think I found the solution. I created additional LiveData field filterChangeEvent . Every time the user changes filtering order, new value is set to filterChangeEvent . Then I use switchMap with filterChangeEvent as a trigger:

    observableTasks = Transformations.switchMap(filterChangeEvent, input -> {
        final MediatorLiveData<List<Tasks>> result = new MediatorLiveData<>();
        result.addSource(tasksRepository.getTasks(), tasks -> result.setValue(filterTasks(tasks)));
        return result;
    });

Based on the information you have provided, I did following steps, and my observable got triggered as expected. I think you're doing something wrong either in repository or in class where you handle the transformation. As there's not enough code to check, I created my own dummy classes:

  1. I created dummy Task POJO:
public class Task {
    private String id;

    public Task(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @NonNull
    @Override
    public String toString() {
        return "Task id is " +id;
    }
}
  1. I created dummy repository which has LiveData:
public class TasksRepository {
    private List<Task> taskList = new ArrayList<>();
    private MutableLiveData<List<Task>> _tasks = new MutableLiveData<>();
    private LiveData<List<Task>> tasks = _tasks;

    public TasksRepository() {
        for (int i = 0; i < 5; i++) {
            taskList.add(new Task(createTaskId()));
        }
        _tasks.setValue(taskList);
    }

    private String createTaskId() {
        return UUID.randomUUID().toString();
    }

    public void addTask() {
        taskList.add(new Task(createTaskId()));
        _tasks.setValue(taskList);
    }

    public LiveData<List<Task>> getTasks() {
        return tasks;
    }
}
  1. Created class called MyViewModel which can handle transformation. During transformation we just add "TEST" prefix to task id. You can compare it with your code and it should be fine:
public class MyViewModel {
    private final LiveData<List<Task>> observableTasks;

    public MyViewModel(TasksRepository tasksRepository) {
        this.observableTasks = Transformations.map(tasksRepository.getTasks(), this::changeId);
    }

    private List<Task> changeId(List<Task> tasks) {
        List<Task> resultTaks = new ArrayList<>();
        for (Task task : tasks) {
            String newId = "TASK" + task.getId();
            task.setId(newId);
            resultTaks.add(task);
        }
        return resultTaks;
    }

    public LiveData<List<Task>> getObservableTasks() {
        return observableTasks;
    }
}
  1. Add data on when button clicked and observe data changes:
TasksRepository tasksRepository = new TasksRepository();
MyViewModel viewModel = new MyViewModel(tasksRepository);

button.setOnClickListener(v -> tasksRepository.addTask());

viewModel.getObservableTasks().observe(this,
        tasks -> Log.d(TAG, Arrays.toString(new List[]{tasks})));

Checkout Transformation.switchMap. It explains following case when to use it also.)

Scenario:

In a scenario where the repository contains User(1, "Jane") and User(2, "John"), when the userIdLiveData value is set to "1", the switchMap will call getUser(1), that will return a LiveData containing the value User(1, "Jane"). So now, the userLiveData will emit User(1, "Jane"). When the user in the repository gets updated to User(1, "Sarah"), the userLiveData gets automatically notified and will emit User(1, "Sarah").

When the setUserId method is called with userId = "2", the value of the userIdLiveData changes and automatically triggers a request for getting the user with id "2" from the repository. So, the userLiveData emits User(2, "John"). The LiveData returned by repository.getUserById(1) is removed as a source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM