Does the UseCase always necessary to have a `Corou...
# codereview
m
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
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
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
agreed
👌 1