-
Notifications
You must be signed in to change notification settings - Fork 15
CoroutineContext.effect doesn't switch back to caller thread #22
Comments
That's why I preferred the |
@pakoito The thing is that this approach would be pretty handy for mobile or UI apps since it saves you from doing (or forgetting) val result1 = fx {
!effect { setViewLoading() }
continueOn(NonBlocking)
val data = !effect { retrieveSomeData }
continueOn(Main)
!effect { showDataInView(data) }
} It is actually one of the reasons why I had to create this 2 dumb extensions in my experiment, otherwise I might forget switching a thread at some point and get some unexpected exceptions. Although I'm not sure how complex would be to have that behaviour described in the issue. |
Forgetting to go back to the The more I think about adding a mandatory |
We definitely don't want to "pollute" the lib API as is just because of Android @pakoito. Actually on my mind this was more about adding it as sugar in the |
Sorry I didn't read this before submitting arrow-kt/arrow#1395 😅 I agree, if this is not a need for non-Android apps (not sure about desktop JavaFx and so) then I agree it doesn't make sense to pollute the arrow-effects core with it. For me it's perfectly fine to stall the PR and wait for the android module 👍. It could also allow us, as @pakoito suggested, to use this assumptions to, for example, default to the Main thread after an effect 👌 |
You're PR could also be a good excuse to add an early |
+1, I've seen some really big messes with implicit threading. It can turn in a very big price tag, $ and CPU wise. I personally prefer this to be as explicit as possible. It's not typed, so I need to dive into the functions to see what they do etc. We optimize IO.ContinueOn(DatabaseCtx)
.flatMap { IO.ContinueOn(NetworkCtx) }
.followedBy(IO.ContinueOn(Main)) == IO.ContinueOn(Main) This might degrade depending on the solutions used. The more complex the solution is, adding a case to the Another case against or an alternative solution which I'd prefer. Is was something @raulraja came up with a while ago. I am not sure what the plans are with it tho. |
There's no such thing with coroutines. If the current context is EmptyCoroutineContext, the default, using it won't make it come back to the same thread. It requires installing event loops on arbitrary threads to make it work end-to-end, and IIRC we don't have those, although it rings a bell that we did something similar recently. I believe RxJava removed theirs for some reason. |
I think we should focus the discussion in one place, if possible. |
So, is there a decision on this? so we can try to move forward. |
Simon changed to event loops in Fx/EnvIO, but that's implementation-specific. We'll need to revisit it properly. |
I had a small discussion with @JorgeCastilloPrz on Slack the other day. We'd propose this API, and thus remove
Separate discussion: should racing and parMap return to the original context? |
RxJava doesn't do any event looping and thus also never returns to an original thread so brining |
Looks good to me. I'm always up for simpler API surfaces instead of having N solutions for "the same thing". @nomisRev would there be any way of ensuring evalOn can just be called over |
You'll need to make The |
Implementing @JorgeCastilloPrz it doesn't really make a lot of sense to me since a lot of people will still just work with Also I am not sure I understand what you mean with
As in do a runtime check to avoid redundant jumps? That would be really confusing because some people might want to do excessive switching because of fairness. We do this optimization in |
👍 fair point
Nono I didn't wanna to go that deep, it was simpler than that tbh. It was more in the line of having a way to ensure people can't make thread jumps work in fx in any way for suspended functions that are not wrapped in
|
This requires an EventLoop similar to KotlinX's EventLoop in |
Closed due to obsolete |
Maybe this is a self-assumption but I think it would be really helpful to avoid multiple thread switches, specially on "UI-driven" frameworks such as Android.
Quick example, having:
At least from a newbie point of view I'd expect the output to be (when called from the main thread):
But actually is:
This is due to the
effect
ext fun behaving similarly tocontinueOn
, although I'd expect that ideally only the lambda inside is executed in theNonBlocking
context and then returning to the caller context (main
in this case).The text was updated successfully, but these errors were encountered: