Answer the question
In order to leave comments, you need to log in
CoroutineScope(Dispatchers.Main).launch{} vs CoroutineScope(Dispatchers.Main).launch(DispathersMain){}?
1. I don’t quite understand why the name of the coroutine should be specified in the constructor, because then you can’t specify the execution flow for it, you can only at launch, and the name of the coroutine is shown simply in the logs.
val scope1 = CoroutineScope(CoroutineName("MyCoroutine"))
//либо так
scope1.launch{}
//либо так
scope1.launch(Dispatchers.Main){}
val scope2 = CoroutineScope(Dispatchers.Main)
//либо так
scope2.launch{}
//либо так
scope2.launch(Dispatchers.Main){}
Answer the question
In order to leave comments, you need to log in
1) the name of the coroutine is needed only for debugging
2) there is no launch constructor, it's just a function on the CoroutineScope. There is no difference between these examples. You can transfer a dispatcher if you want to launch to another dispatcher.
3) this is like a repetition of the second question. I'll expand a bit. If a dispatcher (D) is passed to CoroutineScope, then all launches without additional dispatchers will occur on dispatcher D. And if not passed, then all such launches will occur on Dispatchers.Default, that is, for android - on a dispatcher that has several threads, and on which you can perform heavy calculations.
Launches to which a dispatcher is passed will be executed on that dispatcher.
Something like this. This is actually a complex and confusing topic. Behind the external simplicity lies a very complex machinery. You've covered some pretty simple stuff so far.
I found the answer to the first question, the context can be combined with the name through +
val scope1 = CoroutineScope(CoroutineName("MyCoroutine")+Dispatchers.IO)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question