https://kotlinlang.org logo
Title
p

Pablo

10/01/2020, 7:32 AM
Because if it's like this, if for instance I have a CoroutineDispatcher injected that is the IO and the CoroutineScope tha tis the
MainScope()
and I use it like :
@Inject constructor(
...
...
private val mainScope : CoroutineScope,
private val ioDispatcher: CoroutineDispatcher,
...
...
)
And then using it like this
mainScope.launch{
  ...
  result withContext(ioDispatcher) {...}
  ...
}
If I do
Dispatchers.setMain(TestCoroutineDispatcher())
does it change also the
ioDispatcher
? On my test I'm doing it like this :
private val testDispatcher = TestCoroutineDispatcher()
private val mainScope = TestCoroutineScope(testDispatcher)
And then on the
@Before
I'm using this
Dispatchers.setMain(testDispatcher)
And on every test I'm using
mainScope.runBlockingTest{...}
or
testDispatcher.runBlockingTest{...}
Is something I could improve?
g

gildor

10/01/2020, 7:44 AM
does it change also the 
ioDispatcher
No, there is no such mechanism for other dispatchers
👍🏽 1
private val testDispatcher = TestCoroutineDispatcher()
private val mainScope = TestCoroutineScope(testDispatcher)
It’s redundant, you can just use TestCoroutineScope()
p

Pablo

10/02/2020, 12:39 PM
Sorry Andrey, I was reading again and what you think is redundant I got it, but it is a problem with my DI? I mean I have an inject for the CoroutineScope and one for the CoroutineDispatcher so that's why on my test I create both to pass them as a parameter, is that ok?
What I mean is on my test I have to create my presenter and now I was doing this :
private val testDispatcher = TestCoroutineDispatcher()
private val mainScope = TestCoroutineScope(testDispatcher)

...
presenter(...,mainScope,testDispatcher)
This is still redundant?
g

gildor

10/03/2020, 8:16 AM
If your dispatcher argument is something like ioDispatcher, so it explicitly requests some dispatcher to manage IO, then it's not redundant
But most of code, which doesn't require CPU or IO related dispatchers explicitly, probably could work only with scope
p

Pablo

10/03/2020, 8:18 AM
On my presenter you mean? I'm injecting CoroutineScope as MainScope() and CoroutineDispatcher as Dispatchers.IO so, I show the progress in the main thread and then change it to IO to do api request and go back to main
g

gildor

10/03/2020, 1:29 PM
Yep, makes sense to inject IO separately, if you need it, but not for api request
Api request should be already run on 9wn thread what kind http client do you use?
p

Pablo

10/07/2020, 9:36 AM
Still with some doubts about that check my last message please @gildor :((
g

gildor

10/07/2020, 9:43 AM
what kind doubts?
p

Pablo

10/07/2020, 9:48 AM
I've asked another question on the bottom of the channel see please