althaf
09/17/2022, 7:11 AMjava.lang.IllegalArgumentException: Flow context cannot contain job in it. Had [SupervisorJobImpl{Active}@7b4ca1f, <http://Dispatchers.IO|Dispatchers.IO>]
interface UseCaseScope : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = SupervisorJob() + <http://Dispatchers.IO|Dispatchers.IO>
}
The intention here is to cancel the coroutine context when we business logic need to cancel it. So that i can call coroutineContext.cancel()
class LoginUseCase(private val authRepository: AuthRepository) : FlowUseCase<LoginRequest, Boolean>() {
override fun execute(parameter: LoginRequest): Flow<Result<Boolean>> =
flow {
authRepository.login(parameter.username, parameter.password).collect { response ->
this.defaultResultHandler(onSuccess = {
emit(Result.success(true))
}, response)
}
}.flowOn(coroutineContext)
override fun cancel() {
coroutineContext.cancel()
}
}
^^^ will cause app the crash
Or is this is the way to execute my intention, and ignore flowOn(.....) , i'm really confused here
class LoginUseCase(private val authRepository: AuthRepository) :
FlowUseCase<LoginRequest, Boolean>() {
override fun execute(parameter: LoginRequest): Flow<Result<Boolean>> =
flow {
>>>> launch(coroutineContext) {
authRepository.login(parameter.username, parameter.password).collect { response ->
this@flow.defaultResultHandler(onSuccess = {
emit(Result.success(true))
}, response)
}
}
}
override fun cancel() {
>>> coroutineContext.cancel()
}
}
muzamil
01/23/2023, 7:30 AM