I"m not able to use coroutine Context, in the flow...
# flow
a
I"m not able to use coroutine Context, in the flowOn(....), app is crashing with
java.lang.IllegalArgumentException: Flow context cannot contain job in it. Had [SupervisorJobImpl{Active}@7b4ca1f, <http://Dispatchers.IO|Dispatchers.IO>]
Copy code
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()
Copy code
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
Copy code
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()
    }
}
m
you never need to do that on flowOn. flowOn take dispatcher. (Dispatchers.Default, Dispatchers.main,Dispatchers.IO…)
1024 Views