Hey there, i have already asked this in the <#CRJC...
# android
a
Hey there, i have already asked this in the #flow its been 3 days without answer, so i'm posting it here 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>
}
Copy code
FlowUsecase : UseCaseScope {
 <snip>
}
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
c
the error states
Flow context cannot contain job in it
. Therefor you should not be passing a job to the
.flowOn
operator. Instead pass the dispatcher
if you want to cancel the flow collection, you should cancel the job which consumes the
execute
flow
so in some class that consumes the
LoginUseCase
you would have:
Copy code
class MyClass(
    private val loginUseCase: LoginUseCase
) {
	fun doWork() {
		scope.launch {
		    loginUseCase.execute(...)
		        .collect()
		}
	}

	fun cleanup() {
	    scope.cancel() //
	}
}
a
@Ciaran Sloan thank you