In work manager’s doWork function, what scope can ...
# android-architecture
v
In work manager’s doWork function, what scope can we use to run a flow?
Copy code
override suspend fun doWork(): Result {
    myFlow.onEach { }.launchIn(//What scope do we use here?)
}
n
I think
CoroutineWorker
handles this for you if you do something like the following:
Copy code
override suspend fun doWork(): Result = myFlow.collect { }
CoroutineWorker
launches
doWork
in it's own scope but doesn't make that scope publically visible, so if you just return your flow I'm pretty sure
CoroutineWorker
should just handle it for you. Check out the code here: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-master-dev:work/workmanager-ktx/src/main/java/androidx/work/CoroutineWorker.kt;bpv=0;l=63
s
Why would one want to be aware of the used coroutine scope ?
s
You don't need launchIn. The launchIn changes the scope in which the Flow is emitting items. If launchIn is not specified, this scope is the same scope in which the Flow's 'collect' is called. When 'collect' is called within a 'suspend' fun, the scope is the calling/outer CoroutineScope.
👏 2