Hello dear Android developer community, I have a q...
# android
a
Hello dear Android developer community, I have a question regarding testing with coroutines in Android. What is the best aproach to take when injecting repositories, usecases, etc. with coroutine dispatchers and/or coroutine scope? What is better to inject and why: a CoroutineDispatcher or a CoroutineScope which would be a dispatcher plus a job?
m
Hi @Alexandru Caraus When it comes to testing with coroutines in Android, the decision between injecting
CoroutineDispatcher
and
CoroutineScope
depends on your specific use case and requirements.
g
It depends on the case, but I would say if you inject only dispatcher. you shouldn't be able to launch new coroutines, class should have only suspend functions (otherwise you break structure concurrency)
Maybe easier would be to think about it as: 1. If I need to do some work on particular dispatcher (because it's heavy CPU, or maybe some kind limited database pool etc), inject dispatcher, but often coroutines code can decide for itself, if Default/IO/Main is enough for you and just use Dispatchers and no need to inject 2. If you want to launch a new background coroutine, you have to inject scope If your class includes only suspend functions, no background job and default dispatchers covers your use cases, no need to inject anything
a
Thank you for the detailed reply!
👍 1