https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
v

voben

02/11/2020, 9:28 PM
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

Nicholas Doglio

02/11/2020, 9:41 PM
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

satyan

02/11/2020, 9:54 PM
Why would one want to be aware of the used coroutine scope ?
s

streetsofboston

02/12/2020, 2:27 AM
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
15 Views