Implementing Interactors with Android MVP Clean Architecture

I am currently creating an Android application and want to base it on a "clean architecture" similar to the suggestion of the following authors:

Current architecture:

View (snippet) ↔ Presenter ↔ Interactor ↔ Repository

  • The fragment implements the presentation and creates the presenter.
  • The presenter refers to the fragment via the View interface.
  • The facilitator implements the Interactor.Callback interface for the data that will be presented.
  • The facilitator creates and runs Interactor.
  • Interactor retrieves and updates data to execute business logic in the background thread from the repository.
  • The interactor implements Repository.Callback for DB / Server data from the repository.
  • Interactor is registered in the update repository for the required data.

In the current project, there is 1 interactor per display (the display may include several fragments, for example ViewPager with 30 fragments of the same type) and 1 presenter per fragment. Presenter and Interactor have no structure dependencies to provide easy testing.

My main problem is the implementation of Interactors / UseCases and their relation to presenters (MVP) or ViewModel (MVVM).

Problem:

It is planned that Interactor will first display all the necessary business objects (BOs) for display. The extraction is carried out synchronously from the data layer, and each of them receives a BO sent to the Presenter. This causes a delay until all data is shown in the view.

He then registers for updates to the BOs that he is interested in (as before) to constantly update the presentation through presenters.


So I'm looking for guidance on creating Interactor in my case. The implementation mentioned above has one task, then complete it, and the Interactor background thread may be closed.

In my case, Interactor registers for updates from the datalayer and waits for their processing, and then sends the data to the Presenter user interface stream, so it lives until the presenter listens.

This functionality is different, and I'm looking for good practice to make it work with "clean architecture".

+5
source share
1 answer

So, if I understand your question, your concern or doubt arises from the fact that your Interactor is not going to complete the task and then ends, but they sign it or listen to it until the operation is completed.

In my opinion, excellent, Interactor implements a use case, and in your program that an asynchronous request is a use case, it does not matter if it takes time and is an asynchronous task or a synchronous operation.

This is still a precedent when the Presenter will instantiate an Interactor, and when this is completed, it will return the result of the operation. As long as you remain modular, and Presenter and Interactor are not related to direct dependencies, but they exchange data through indirect dependencies, that’s fine.

+1
source

All Articles