A
A
Alexander Shustanov2016-09-01 10:59:10
Java
Alexander Shustanov, 2016-09-01 10:59:10

Can Subjects be used to propagate model changes?

I am developing a fairly heavy, highly loaded application for android. Eats a lot of memory, processes a fairly large amount of data. Development has been going on for more than two years, the architecture has changed more than once during this time, as a result, they came to a combination of flux and rxjava.
In the standard description of flux, nothing is said about multithreading (because it was invented for the web), however, all actions are processed in a separate thread (data thread). This allows you to avoid all sorts of ConcurrentModificationException and so on, especially since some actions are quite heavy and the ui stream simply won't pull. Accordingly, when a view receives dataChanged, it needs to receive data from some store. Since the work with the model is carried out in the data thread, it is simplystore.findSomeEnitityById(id)can't be done. Instead, you need

store.findSomeEnitityByIdObservable(id).observeOn(AndroidSchedulers.mainThread()).subscribe(...)

Imagine that the model changes frequently. DataChanged comes quite often and findSomeEnitityByIdObservable are created more often than the old ones have time to work out. In addition, some other state changes in the store we are interested in, which is not of interest to the current view. The following option came to my mind.
In each store, for each minimal piece of the model that some view may need, a BehaviorSubject is created. for example
BehaviorSubject<EntityList> entities = BehaviorSubject.create();

The changes are received like this:
//Метод в сторе
Observable<Entity> findEntityById(long id, long milliseconds) {
    return
        entities.throttleLast(milliseconds, TimeUnits.MILLISECONDS).map(entities -> entities.findById(id));
}
//Вьюшка
void onResume() {
    subscription = store.findEntityById(id, 1000).observeOn(AndroidSchedulers.mainThread()).subscrive(...);
}
void onPause() {
  subscription.unsubscribe();
}

It seems to me that the plus is that instead of creating new Observabe every time we get dataChanged, we create one in onResume, and we can adjust the frequency of updates. In addition, dataChanged tells us that the state of the store has changed, but its state can have quite a few aspects (which can not always be spread across other stores), and dataChanged does not at all say that exactly what we need has changed.
The question is, am I doing the right thing, and are there other solutions to the problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question