https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
a

amatkivskiy

05/30/2019, 4:11 AM
Hi, I am trying to migrate my app architecture (which is basically Clean Architecture based on RxJava) to Clean Architecture based on kotlin coroutines. There is one thing that I am still struggling is coroutines dispatchers. Previously with RxJava it was common to place main thread and background Schedulers in UseCase and then use them when creating Observable. Right now I see that in some cases developers put coroutine dispatchers in UseCase and then triggering coroutine with specific dispatchers. In other cases I saw that UseCase was made basically just a suspend function and all work related to dispatching was moved to the caller of the UseCase. Any ideas what is more correct way to do this?
s

sergio250

05/30/2019, 7:55 AM
I’d say letting the caller decide the execution context is often a better solution for two reasons, this way you don’t have to differentiate between sync/async UseCases, and you can run some potentially expensive computations (like mapping domain to UI models) in a background thread from the caller
a

amatkivskiy

05/30/2019, 12:23 PM
@sergio250 thanks, I was also more to this approach
s

sergio250

05/30/2019, 12:29 PM
I think I have some (very basic) example of this approach in a Kata we use for UI testing in my company
Found it, UseCases are not suspended though 🤔
a

amatkivskiy

05/30/2019, 12:30 PM
let me have a look
o

oday

05/30/2019, 2:32 PM
why don’t you join #coroutines
a

amatkivskiy

05/31/2019, 3:55 AM
@oday because this topic relates more to general architecture rather than coroutines
@sergio250 I found one possible cause that may require dispatchers : when parallel work is required (like making multiple network simultaneously ) then you will need context to properly use async await stuff
s

sergio250

05/31/2019, 8:02 AM
You are right 🤔
a

amatkivskiy

05/31/2019, 1:57 PM
On the other hand there is
coroutineScope {}
which as far as I understood creates a child scope based on the parent and allows to call async await coroutines
s

Sam

06/04/2019, 12:46 AM
Yes, you can have an extension on CoroutineScope when launching coroutines
6 Views