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

trathschlag

05/18/2020, 11:29 AM
Copy code
fun scroll() = flow {
  try {
    // do stuff
  } finally {
    withContext(NonCancellable) { suspendingCleanUpFunction() }
  }
}
This does obviously not work. What would be the correct pattern here?
m

marstran

05/18/2020, 11:46 AM
Why doesn't it work? Not too obvious to me 😛
t

trathschlag

05/19/2020, 6:13 AM
Ah I am sorry. I had an
emit()
in some other place inside a
withContext(NonCancellable)
So is the above considered ok?
e

elizarov

05/19/2020, 7:04 AM
Yes. It is Ok. But you-might save some nesting by rewriting it like this:
Copy code
fun scroll = flow { /* doStuff */ }
    .onCompletion { /* do finally */ }
2 Views