Ad

Where Should I Chain Multiple Network Requests In MVVM? UseCases Or Repository?

I am refactoring our codebase. Right now we have a singleton Repository which ceates network requests and manages operators chaining with RxJava and Kotlin Coroutines.

However that Repository is being injected into multiple ViewModels and becomes harder to mantain.

I would like to abstract our codebase and integrate some useCases. As far as I know it is general opinion to use a single network request in useCase, this is why I am unsure about chaining network requests.

I need to chain multiple network requests using Observable.zip and RxJava Subjects.

Would the following flow be correct approach?

Viewmodel calls useCase, which creates network request(from repository), handles that network request, creates a LiveData and forwards that LiveData to ViewModel and also updates subject from the Repository, so that Repository could emit some data after all network requests have finished?

Then our Repository remains similar to our current approach with less code and will only be responsible for multiple network requests chaining.

I hope I was clear enough. I have investigated multiple MVVM examples, but they seemed not practical for real applications, with complex networking logic.

Ad

Answer

You should not chain different API calls in repository. Mapping to more complex domain objects should be done in a use case. Usually there is more than one repository, each of them is responsible for some part of business domain models (e.g. UsersRepository, PostsRepository, etc). Use case can depend on multiple repositories.

Example (Plaid app)

Ad
source: stackoverflow.com
Ad