Hello! Can anyone tell me if there's a neater way of doing this? I'm trying to essentially run some code after a specified delay, but I want to handle the case the function is called before it's actually finished executing.
Copy code
private var job: Job? = null
private fun scheduleJob() {
job?.cancel()
job = flow<Void> {
delay(mySpecifiDelay)
doWork()
}.launchIn(CoroutineScope(coroutineDispatcher))
}
d
Dominaezzz
07/23/2021, 5:07 PM
Might as well be using
GlobalScope
there.
Dominaezzz
07/23/2021, 5:08 PM
Also, it doesn't look like you need a flow here, you can just
Yeah I've been going back and forth on the best way of handling scopes, I'm injecting the coroutineDispatcher and I can never decide which pattern I prefer when it comes to getting hold of a scope
d
Dominaezzz
07/23/2021, 5:12 PM
Well, if you're going to re-create it each time, just use
GlobalScope
. It's the same thing but less allocations.
a
andylamax
07/25/2021, 10:44 AM
Instead of injecting Dispatchers, I inject CoroutineScopes.
It has been helping me much. Including having it in memory