https://kotlinlang.org logo
#codereview
Title
# codereview
m

miqbaldc

12/13/2021, 12:07 PM
Does the UseCase always necessary to have a
CoroutineDispatcher
and returns
Flow<T>
or
suspend
(return
T
) is enough?
Copy code
abstract class FlowUseCase<out T, in Params> constructor(
    private val threadExecutor: CoroutineDispatcher
) {
    protected abstract fun execute(params: Params? = null): Flow<T>
    open operator fun invoke(params: Params? = null): Flow<T> =
        execute(params).flowOn(threadExecutor)
}
What implementation did you folks use, would love to know the pros n cons
m

Marcello Galhardo

12/13/2021, 12:22 PM
I believe the question is answered in the Coroutines Best Practices: 1. The ViewModel should create Coroutines: https://developer.android.com/kotlin/coroutines/coroutines-best-practices#viewmodel-coroutines 2. The business and data layer should expose suspend functions and flows: https://developer.android.com/kotlin/coroutines/coroutines-best-practices#coroutines-data-layer
🙏 1
android new 1
today i learned 1
s

Stylianos Gakis

01/04/2022, 12:01 PM
As a side note, I would argue that this abstract UseCase class doesn’t provide you with any benefits. Why not simply have each UseCase be its own class, without some sort of hierarchy, do you ever use them as their base class? Doesn’t this params: Params? = null make them simply worse to use? Sorry for being off-topic but I really think this will only make your life harder for no reason.
✔️ 5
m

miqbaldc

01/05/2022, 5:24 AM
agreed
👌 1
24 Views