A
A
Alexander Shustanov2016-07-25 08:46:09
Android
Alexander Shustanov, 2016-07-25 08:46:09

How to update model with RxJava in Android?

Habr is full of articles about happiness with rxJava and MVP, where we asynchronously load a model from remote storage (file system, server). But there is absolutely no description of how this model changes in response to user actions.
Example, https://habrahabr.ru/company/rambler-co/blog/277003/.Description of the VIPER architectural approach. I do not use VIPER myself, but it uses MVP, and in the rambler example, rxJava is also used. The article describes an application that displays messages that it loads from somewhere. But you can’t write messages, the model appears from somewhere, but the user cannot influence it. And it still needs to be saved to the database. And if in the model we have some relationships between entities and we can catch ConcurrentModificationException if we have several Interactors working in different threads.
In fact, there are hardly any problems on the scale of this application. However, in a real large application, with a large model, things are much more complicated.
Let's say we have a list of some elements - markers that the user can turn on or off to be displayed on a map (some navigation application) - each line has a switch. In addition to markers, the model has a large number of other entities (also geo-objects), and there are a lot of them. To make the UI responsive, you need to change the model in a separate thread. To do this, I made my own Scheduler, which I called DATA_SCHEDULER.
It seems that everything is simple - the Presenter asks the MarkerStore Observable> - markerObservable, after receiving the list, passes it on to be displayed in the MarkerListFragment. Each Marker has a visible field, depending on which the switch is either on or off. What happens when the user clicks on the switch is that it should switch, and an event is sent to DATA_SCHEDULER that the visible field needs to be changed. The operation is quite fast, we very soon get an update of the model in markerObservable, where the visible field of a particular marker has changed its value. But what if DATA_SCHEDULER is loaded? Or will the user start drumming on the switch? Many requests to change the visible field will appear in the queue, which will pull it back and forth, after which updates to the markerObservable will fall.
This is a fictional example, but it describes the pain I am experiencing. I think I'm not the only one who had such problems, and I would like to look at possible solutions. Or examples of more realistic applications that use mvp and rxJava, and work with a more realistic model than most of the "clean architecture" examples on Habré. I googled myself, but didn't google anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zoopolitic, 2016-07-25
@alexander-shustanov

You can always use debounce in case the user is bouncing the switch non-stop. I do not see such conditions under which your scheduler will be loaded so that there are delays in the processing of ui events. But if you really have some kind of highly loaded system, use the ThreadPoolExecutor, especially since rxJava makes it easy to create a scheduler based on an executor: Schedulers.from(Executor)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question