Ana Koridze
Ana Koridze

Reputation: 1777

Chaining multiple use cases

I use Clean Architecture pattern in my app together with MVVM architecture. So I have UseCases for single operations, like for example, LoginUseCase, DownloadAttachmentUseCase etc.

What I am curious about is, what if I want to chain multiple usecases in my viewmodel, do something like, login first and once it succeeds, download an attachment.

Do I need to create another separate UseCase e.g. LoginAndDownloadAttachmentUseCase ?

Upvotes: 7

Views: 4926

Answers (3)

E.M.
E.M.

Reputation: 4547

Only the logic for application business rules should be in use cases; changes to the UI should not change how a use case is implemented. To me, it seems like a combined LoginAndDownloadAttachmentUseCase would only be useful in the context of a specific UI, and therefore should not exist as a use case.

Also, if you were to create combined use cases for all possible scenarios, it would lead to a combinatorial explosion of use case classes as your app becomes more complex.

It's ok to have some logic in ViewModels, especially if it's just transforming data or doing high-level operations. Each use case represents a single high-level operation. Having the ViewModel stitch together a few of these does not make testing and maintainability more difficult.

I think https://github.com/googlesamples/android-architecture/tree/usecases is a good example. The use cases are pretty slim, but in more complex apps, they could combine several datasources together.

Upvotes: 7

Sanjay Bhalani
Sanjay Bhalani

Reputation: 2484

It would be good to do separate UseCase because MVVM with Clean Architecture is pretty good in such cases. It goes one step further in separating the responsibilities of your codebase. It clearly abstracts the logic of the actions that can be performed in your app.

Upvotes: 0

Rishabh Jain
Rishabh Jain

Reputation: 155

Try Retrofit Synchronous Requests, but Synchronous methods are executed on the main thread so they trigger app crashes on Android 4.0 or newer. You’ll run into the **NetworkOnMainThreadException** error. To handle this, execution it in a separated thread such as JobIntent Service and get the response via LocalBroadcast receiver. Upon receiving the status, you could execute your next query for downloading the attachment.

Upvotes: -4

Related Questions