What would you do if, for example, you were required to solve a problem with paging or searching by string. What I mean is, we have RecyclerView or SearchView , the user will pop out the list or enter text.

What should we do? Throw events into the презентер , then the презентер calls the интерактор user for each event and receives new data. Or the second option is obtained in View Observable<Integer> (for pagination) or Observable<String> (for search), which will be issued every time when the list or string of search is changed in View . Next, give the Observable presenter, who will deliver the Observable as a parameter to the interactor. It turns out that you only need to call the интерактор once. Here is another question, how to control the whole thing, for example, for each change, you need to additionally show something in the View (progress bar, etc.), how best to do it? To hang operators on Observable in a presenter or throw events from the interactor? Which option do you think is conceptually correct, which is more convenient?

    1 answer 1

    I will write not “how to”, but “how I usually do and what are the advantages”.

    In the case of a search line and a list, it's really nice to track events by subscribing to them as Observable . I can not find where, but in some of the conferences saw a report on this approach. Here, only (yes, as in all Rx) we must not forget to unsubscribe. The method is also good because if you quickly change the text or drag the list up and down (often trigger the pagination event), you can use the excellent debounce() and the delays work as you need.

    https://medium.com/@matdziu/using-rxjava-in-searchview-f1d1d5dcb8b7

    Regarding the transfer of Observable between layers. Let there be some data source (some repository , which returns Observable to request data from the database or network). When a user navigates to a specific screen, View notifies the Presenter to load the data, Presenter asks the Observable for Interactor and subscribes to it. Interactor in turn, asks the Repository this Observable and, before returning it to the presenter, hangs various operators if necessary (filters, mappings, etc.). The point is that View only notified the Presenter about the event, and that one, if the data had time to be processed, and the user is still on this screen, will simply transfer them to the View .

    If we have a search string or list, then the data source becomes View . If View itself throws Presenter Observable , this means that a dependency has appeared that is the opposite of the one described above. In my opinion - not an option. I would create an Observable for the search line or list and let Presenter decide when to subscribe and unsubscribe. That is, when changing a line or scrolling, you do not speak to presenter.onChanged(Observable) , but simply when creating a View , create an Observable for the necessary changes on the screen, and Presenter will subscribe / unsubscribe to events via the interface you provide.

    If I understand correctly, then this approach is called reactive work with View . In this way, you can create Observable for any events on the screen (even by simply pressing a button), and Presenter will subscribe when it is necessary. At the same time, View does not manifest any initiative towards Presenter .

    I will be glad to criticism from the guru of architecture in the comments.

    • Thank you for your opinion! I have used this approach for some time, it is really very convenient to use the debounce operator. Only I gave Observable to the presenter, I had some problems with the re-creation of the View Observabl and the re-subscription resulted in an extra emit, I don’t remember the details, but others encountered similar problems with this approach / blob / master / sample-mvi / src / main / java / ... ), and do you propose to keep the Observable in View? - Alexandr
    • The presenter experienced a re-creation at the same time View - Alexandr
    • And you want to keep observable in a presenter, despite the fact that it will issue a twist? Here, after all, not working with data as such, but with a view of events. Such observable in my opinion should be at the source of events. - Vladimir Parfenov
    • Yes, I considered this option. Thanks, I need to think about it again. In the approach of reactive work with View, despite the fact that everything seems simple enough, in practice, something will definitely go wrong =) - Alexandr