Satyam Agarwal
05/01/2021, 2:45 PMCoroutineScope.actor
. But was wondering if I can get something functional.Joram Visser
05/01/2021, 3:00 PMsimon.vergauwen
05/01/2021, 4:30 PMCoroutineScope
and then forget about it.
Fiber
exposed several ways of launching a task in parallel, which are all covered already by Deferred
and CoroutineScope
. I didn't cover so much of this in the docs, because that would mean we'd basically be rewriting docs that belong into KotlinX Coroutines.simon.vergauwen
05/01/2021, 4:31 PMsimon.vergauwen
05/01/2021, 4:32 PMSatyam Agarwal
05/01/2021, 4:36 PMSatyam Agarwal
05/01/2021, 4:38 PMCoroutine.actor
gives this kind of support.
You launch operations to this actor, and then you can read from the channel of this actor to write logs in the order they came.Satyam Agarwal
05/01/2021, 4:38 PMSatyam Agarwal
05/01/2021, 4:39 PMsimon.vergauwen
05/01/2021, 6:00 PMB
?
To fire and forget you could use a locally created CoroutineScope
or GlobalScope
CoroutineScope(Dispatchers.Default).launch {
f()
}
but if you're on Android or any other cancellable framework I think it would be better to use viewModelScope
(of B
) for example or a CoroutineScope
that lives on an App
level rather than a screen level.simon.vergauwen
05/01/2021, 6:16 PMCoroutineScope
would you calling the actor
?
I think I'd just use a Channel
to replace the actor
and consume it as a Flow
.Joram Visser
05/02/2021, 9:57 AMSatyam Agarwal
05/03/2021, 7:56 AMwhat do you do with the result of B ?The result gets evaluated and a state in the database is updated. So once backend gets a request to show new state, this perhaps updated state may be returned.
On what CoroutineScope would you calling the actor?I was thinking :
CoroutineScope(SupervisorJob() + IO).actor
Satyam Agarwal
05/03/2021, 7:57 AMI think I’d just use a Channel to replace the actor and consume it as a Flow.You mean some thing like this : https://kotlinlang.org/docs/flow.html#buffering
simon.vergauwen
05/03/2021, 2:49 PMThis all is a backend systemOh, then things are a bit simpler 🙂 What back-end are you using?
CoroutineScope(SupervisorJob() + IO).actor
is not a great option, since this is not tied to any lifecycle. You'd probably want to hook this into the lifecycle of your back-end, and preferably suspending even on close
so that the back-end closes gracefully.Satyam Agarwal
05/03/2021, 3:15 PMCoroutineScope(SupervisorJob() + IO).launch {
f()
}
like you suggested. But then this CoroutineScope
is not connected to ktor, no.
How do you suggest I achieve it ?Satyam Agarwal
05/03/2021, 3:19 PMSatyam Agarwal
05/03/2021, 3:24 PMval appScope = CoroutineScope(SupervisorJob() + IO)
and use appScope
everywhere. Since it is not tied to a parent scope, I fear it can get garbage collected ?
Thinking to make singleton like :
object BackgroundScope : CoroutineScope {
override val coroutineContext: CoroutineContext = SupervisorJob() + IO
}
simon.vergauwen
05/03/2021, 3:27 PMKtor
does have a (parent) Application
scope available though. https://api.ktor.io/1.5.4/io.ktor.application/-application/simon.vergauwen
05/03/2021, 3:27 PMCoroutineScope
and then you should be good 🙂simon.vergauwen
05/03/2021, 3:29 PMSharedFlow
and afaik you don't need it since Ktor has full support for suspend
everywhere.
In such cases (just like with IO
) I prefer data typs that offer me proper strategies such as bounded, slide, drop etc.Satyam Agarwal
05/03/2021, 3:31 PM