reactormonk
10/09/2025, 12:36 PMDmitry Khalanskiy [JB]
10/09/2025, 12:41 PMwithContext(NonCancellable) {
// this will run even if the caller is cancelled
}reactormonk
10/09/2025, 12:42 PMcollectLastest wait for the cancellation to go through, or would I need to lock that manually?Dmitry Khalanskiy [JB]
10/09/2025, 12:43 PMreactormonk
10/09/2025, 12:45 PMflowOf(1,2,3).collectLatest { i -> withContext(NonCancellable) { sleep(1.seconds); println(i) } }
^ will this take 3 seconds, or 0 to finish?Dmitry Khalanskiy [JB]
10/09/2025, 12:47 PMreactormonk
10/09/2025, 12:47 PMDmitry Khalanskiy [JB]
10/09/2025, 12:48 PMcollectLatest collects the latest, and in the code you've provided, 1 and 2 are clearly not the latest and can be instantly skipped.reactormonk
10/09/2025, 12:50 PMcollectLastest as part of a system where it reacts to changes from the outside world, so basically
.collectLastest { stateX, stateY, stateZ ->
... execute some code based on it
}
and cancel/restart as needed once the status changes. So you're saying I should explicitly lock here for future versions of coroutines?Dmitry Khalanskiy [JB]
10/09/2025, 12:54 PMcollectLatest for its intended purpose, and I still don't see why you'd want the answer to the above to be three seconds.
Here's why we intend to make it 1 second:
• Value 1 arrives.
• Wait, don't process 1! Another value is about to arrive.
• Value 2 arrives.
• Wait, don't process 2! Another value is about to arrive.
• Value 3 arrives.
• sleep(1.seconds) and println(3) are called.
Do you actually want to still start processing 1 and 2 even if you know in advance that another, more recent value is about to arrive?reactormonk
10/09/2025, 12:55 PMDmitry Khalanskiy [JB]
10/09/2025, 12:55 PMcollectLatest lambda was entered, the answer would be 3 seconds.reactormonk
10/09/2025, 12:56 PMDmitry Khalanskiy [JB]
10/09/2025, 12:57 PMDmitry Khalanskiy [JB]
10/09/2025, 12:57 PM1, and while that's happening, 2 would arrive, be replaced by 3, and then 3 would be processed.Dmitry Khalanskiy [JB]
10/09/2025, 12:58 PM1 wouldn't be interrupted once it's started, though.reactormonk
10/09/2025, 1:20 PM