Android-CleanArchitecture-Kotlin icon indicating copy to clipboard operation
Android-CleanArchitecture-Kotlin copied to clipboard

Questions: How to combine multiple UseCase?

Open rommansabbir opened this issue 4 years ago • 4 comments
trafficstars

Scenario:

  • I have 3 UseCase to perform 3 different API Request.
  • Based on the 3 UseCase response I need to combine them all according to the business logic.

Right now, I'm doing it by calling the 1st UseCase, then based on response, then another & so on.

So, how can I combine/zip them all in a single request or chain them one by one in a proper way?

rommansabbir avatar May 20 '21 14:05 rommansabbir

This can be solved by Coroutines, Async and Await

  • Async and Launch is the coroutine builder, Launch coroutine builder returns a job but async coroutine builder returns an instance of Deferred.
  • We can use that Deferred value by invoking its await function

CoroutineScope(Main){ val stock1 = async(IO){ getStock1() // Api Call }val stock2 = async(IO){ getStock2() // Api Call }val total = stock1.await() + stock2.await() Log.i(“total”, total) }

  • This is how we use async and await to get data from different data sources parallel and combined the result

On Thu, May 20, 2021 at 7:38 PM Romman Sabbir @.***> wrote:

Scenario:

  • I have 3 UseCase to perform 3 different API Request.
  • Based on the 3 UseCase response I need to combine them all according to the business logic.

Right now, I'm doing it by calling the 1st UseCase, then based on response, then another & so on.

So, how can I combine/zip them all in a single request or chain them one by one in a proper way?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/android10/Android-CleanArchitecture-Kotlin/issues/111, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIYEFBVIEAECQ4J45LTOLTTOUJUXANCNFSM45HDCB7A .

--

Thanks, Pankaj

pankajpy avatar May 20 '21 16:05 pankajpy

@pankajpy actually, I'm talking about how to merge/combine multiple UseCase into one. Yes, I know how to merge multiple API calling using Coroutine but UseCase is missing in this scenario.

rommansabbir avatar May 20 '21 18:05 rommansabbir

Have a try to combine response in the Repository layer and then pass the Repository to a new UseCase.

lrnrzg avatar May 23 '21 05:05 lrnrzg

@ZhuSniffsTheRose Yes got your point. But the scenario is:

  • Have 3 Different Repository for Different Purpose
  • Have 3 Different Use Case for 3 Different Purpose

Now, I can call all required api one by one in a single use by adding their dependency, but I want to combine merge/combine multiple UseCase into one.

rommansabbir avatar May 26 '21 15:05 rommansabbir