https://kotlinlang.org logo
#coroutines
Title
# coroutines
l

Luke Rohde

12/03/2019, 6:25 PM
is there anything wrong with doing cleanup like this in a flow builder block?
Copy code
flow {
  for (i in collection) {
    try {
      emit(i)
    } finally {
      doCleanupOn(i)
    }
  }
}
is there a better way to accomplish this?
o

octylFractal

12/03/2019, 6:34 PM
I'd say that this is the preferred way to do it
l

Luke Rohde

12/03/2019, 6:35 PM
cool. and this will have the semantics that the suspension will only continue once whatever downstream of this call to emit is completed?
or put another way, when does the suspension from emit continue
r

Ricardo

12/03/2019, 6:36 PM
AFAIK, it's not a good practice to have your
emits
between
try-catch
You can use the
.catch
operator to catch any exception from the upstream
If you leave that
try-catch
, you'll be risking funky behaviour like catching an exception thrown by an operator in the downstream, or even thrown by the terminal operator
Then again, you're using
finally
and no
catch
. You don't expect any exceptions?
l

Luke Rohde

12/03/2019, 6:38 PM
actually thinking through this i don’t want the finally
i think i just want emit then cleanup
r

Ricardo

12/03/2019, 6:39 PM
There's an
onCompletion
operator
And yeah, if you don't expect any trouble there, I guess you could just do the cleanup right after the emission
l

Luke Rohde

12/03/2019, 6:40 PM
is onCompletion equivalent to that?
oh no, bc that doesn’t give access to the element
which is what i’m lookign for
r

Ricardo

12/03/2019, 6:42 PM
onCompletion
does the same as having your flow's collection inside a
try-finally
l

Luke Rohde

12/03/2019, 6:42 PM
except it doesn’t have the element in scope
r

Ricardo

12/03/2019, 6:42 PM
Yup
If you won't have access to those elements at the end, then you can't use it
Can´t you just do
Copy code
flow {
  for (i in collection) {
      emit(i)
      doCleanupOn(i)
  }
}
?
l

Luke Rohde

12/03/2019, 6:44 PM
yeah exactly
that’s what i’m doing
r

Ricardo

12/03/2019, 6:45 PM
Solved then 😛
l

Luke Rohde

12/03/2019, 6:45 PM
assuming the semantics are what i said above, which is that all downstream operations on the flow complete before it resumes to doCleanupOn
thanks for your help!
r

Ricardo

12/03/2019, 6:55 PM
No problem, happy to do so 😊
c

codeslubber

12/03/2019, 9:28 PM
I think this thread is endemic of a problem with coroutines: they present as a way to solve the worst flow problems of concurrency, but do they? or like most things, do they just mean you have to find new ways to accommodate same?
o

octylFractal

12/03/2019, 9:31 PM
I am not sure what you mean by that. In general they claim to solve the issues of non-sequential-like async programming, but being able to
emit(i)
and then immediately call
doCleanupOn(i)
is very sequential-like, so this seems to be an example of coroutines working well towards their goal
c

codeslubber

12/03/2019, 9:34 PM
But there are still catches, and questions of when and where things will happen.. the means the idea of a deterministic sequence is not really there right?
o

octylFractal

12/03/2019, 9:47 PM
I feel like that's more like an question of Flow in particular, which is separate from coroutines. And the model is fairly simple, downstream flows pass in a FlowCollector, the upstream calls
emit(i)
, and in
emit
, the downstream uses the element, processes it, etc. So clearly exceptions from downstream are thrown from
emit
.
catch
's implementation uses
collect
&
emit
,
collect
throws the errors from upstream generation and the
emit
calls, and special handling around emit figures out if exceptions thrown from
collect
are from up or downstream. It's not very complicated at all.
c

codeslubber

12/03/2019, 9:57 PM
One thing is for sure: the solutions, are towers erected in sand. The architecture patterns from 2 years ago looked good, had good MVVM approach, they are mostly dust now, Compose ushered in a whole new approach. You don’t even have time to learn things before they are mothballed. My problem with Rx and its spawns (have not looked enough at flow yet) is that they obscure logic. The argument in the Meyer Bible OOSC is that exceptions are useful for that reason primarily, is pretty compelling, but here we are talking about how you have to figure exactly how to sequence and then gird for upstream emissions. I hope you ARE right because I do think some flow is necessary, but if it overwhelms the code and the logic is every 3rd line, we’re doomed. Might as well go back to C where they had to have if statements on each SDK call, and then block-based handlers for integer return types…
d

Dico

12/04/2019, 7:04 AM
I don't think this is the correct way to do this. The assumption that the element is fully processed after
emit
returns does not apply to some flow operators, namely buffering ones. But it might work in your use case.
👌 1
l

Luke Rohde

12/04/2019, 3:33 PM
is there anything you would recommend short of threading the item through the whole flow?
d

Dico

12/05/2019, 3:11 AM
I dont have an answer.
u

ursus

12/05/2019, 7:16 AM
Hmm, but.. doesnt that presume that the subscriber doesnt switch threads?i.e. if he does, then cleanup will run before subscriber's code, which will then act on a closed instance
feels like the db cursor mapping problem from rxjava, and the result of it was you cannot do it in reactive fashion, you need to map and close it synchronously