I'm having an hard time trying to figure out how m...
# coroutines
t
I'm having an hard time trying to figure out how merging of scope and context works and would appreciate some details ­čÖé
Copy code
class XXX  : CoroutineScope {

	override val coroutineContext = <http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob()

	val aDispatcher =  ThreadPoolExecutor(...).asCoroutineDispatcher()

	internal val tasks = Channel<Task>(UNLIMITED)

    fun startWorkers() {
        repeat(150) {
            launch(aDispatcher) {
				val initialTask = try {
                    tasks.receive()
                } catch (e: Exception) {
                    return@launch
                }
                withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
					// Correctly run on <http://Dispatchers.IO|Dispatchers.IO>
				}
			}
        }
    }
}
Copy code
class XXX  : CoroutineScope {

	override val coroutineContext = <http://Dispatchers.IO|Dispatchers.IO> + SupervisorJob()

	val aDispatcher =  ThreadPoolExecutor(...).asCoroutineDispatcher()

	internal val tasks = Channel<Task>(UNLIMITED)

    fun startWorkers() {
        repeat(150) {
            launch(aDispatcher) {
				val initialTask = try {
                    tasks.receive()
                } catch (e: Exception) {
                    return@launch
                }
                withContext(coroutineContext) {
					// Incorrectly runs on aDispatcher instead of <http://Dispatchers.IO|Dispatchers.IO> that is used for coroutineContext
				}
			}
        }
    }
}
b
In the second example
coroutineContext
is a context from launch(aDispatcher) scope.
coroutineContext
you wanna use is this@XXX.coroutineContext
t
Thanks I feel stupid, I thought they added some warning about unclear context ­čś× I really need some sleep.