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

Remy Benza

10/30/2020, 11:50 AM
How do I cancel a Flow collection and start a whole new flow?
g

Giorgos Neokleous

10/30/2020, 11:54 AM
You can format code blocks with triple ticks
r

Remy Benza

10/30/2020, 11:55 AM
👍 got an idea for my question? 😄
😂 1
l

louiscad

10/30/2020, 12:41 PM
Cancel the coroutine calling collect and launch a new one. The operators of the family xxxLatest might also be what you're looking for @Remy Benza
r

Remy Benza

10/30/2020, 12:42 PM
should I cancel the coroutine scope (tied to the activity) inside the viewmodel?
since I have a date picker which sends an event to the viewmodel with the epoch instant I use to fetch the music tracks
l

louiscad

10/30/2020, 12:43 PM
No, you should not cancel the root scope
You can just cancel the
Job
returned by the call to
launch
or
flow.launchIn(scope)
You can make the user selection a
Flow
and then use
flatMapLatest
r

Remy Benza

10/30/2020, 12:46 PM
so this job I would cancel?
when the user select a date via the date picker?
l

louiscad

10/30/2020, 12:49 PM
Well, I'm not going too deep into your codebase details, but you can convert that listener that reflects user actions (selecting a date) using
callbackFlow
r

Remy Benza

10/30/2020, 12:54 PM
Thnx alot for you replies
But I would need a callback flow in order for this to work right?
Build a callbackflow once for an edittext in a searchbox with a debouncer
l

louiscad

10/30/2020, 12:56 PM
There's multiple ways, but converting the listener to a
Flow
with
callbackFlow
would enable you to use
flatMapLatest
Reading this doc will certainly help you to see what are the building blocks you can leverage: https://kotlinlang.org/docs/reference/coroutines/flow.html
r

Remy Benza

10/30/2020, 1:03 PM
yes thnx @louiscad, I'm quite new to flow as you may have noticed 😋
l

louiscad

10/30/2020, 1:06 PM
Docs are often a great place to start when they're available, as is the case with first party Kotlin libraries 😉
r

Remy Benza

10/30/2020, 1:07 PM
Yes, I've red the docs but then translating these general explanations to your own concrete implementation isn't a piece of cake that often
but your advise helps a lot
l

louiscad

10/30/2020, 1:08 PM
👍 1
r

Remy Benza

10/30/2020, 2:03 PM
ok @louiscad final question 😇, so I wrote the callbackflow extension function for the datepicker but can't seem to use .flatMapLatest
only .map is valid
.flatMapLatest gives me: Type inference failed: inline fun <T, R> Flow<T>.flatMapLatest(crossinline transform: suspend (value: T) -> Flow<R>): Flow<R> cannot be applied to receiver: Flow<Long> arguments: (suspend (Long) -> Unit)
In the .flatMapLatest docs I read: Returns a flow that switches to a new flow produced by transform function every time the original flow emits a value. When the original flow emits a new value, the previous flow produced by 
transform
 block is cancelled.
this is indeed the behavior I'm looking for
l

louiscad

10/30/2020, 2:12 PM
@Remy Benza Which Kotlin version are you using?
r

Remy Benza

10/30/2020, 2:13 PM
Copy code
'1.3.61'
l

louiscad

10/30/2020, 2:13 PM
With Kotlin 1.4.10, a bunch of type inference bugs have been fixed.
r

Remy Benza

10/30/2020, 2:13 PM
can't switch to 1.4 due to very large codebase and kotlinx serialization 0.14 migration work
l

louiscad

10/30/2020, 2:13 PM
kotlinx what?
kotlinx is a not a library
1.4 needs 1.0 for, so we can't just switch over
l

louiscad

10/30/2020, 2:15 PM
This library has been updated as well, and there's a migration guide
r

Remy Benza

10/30/2020, 2:15 PM
I know, the customer has other priorities now so..
l

louiscad

10/30/2020, 2:15 PM
On the long term, it's not really viable to keep using outdated software, so better take the time to upgrade now
But if it's a blocking point for his priorities, then…
r

Remy Benza

10/30/2020, 2:16 PM
Im fully with you on this, but Im not making these decisions
are you sure this is due to a bug?
not a error in my code?
l

louiscad

10/30/2020, 2:16 PM
Anyway, you might be able to get away by specifying all the type arguments to workaround these issues
I don't know, I cannot dive too deep or I'll need a cut from what your client pays you 😛
r

Remy Benza

10/30/2020, 2:17 PM
haha
I'll send them your bill
l

louiscad

10/30/2020, 2:18 PM
Haha, I don't believe it 🙃 Do explicit types work?
r

Remy Benza

10/30/2020, 2:18 PM
but flatMap latest operator should be on the
Copy code
dateSelectionAsFlow()
right? and the canceling the job there is also the right place?
l

louiscad

10/30/2020, 2:20 PM
You can do a test with a simple JVM only playground
To understand how these work
Making a flow is quite easy, there's examples in the doc.
r

Remy Benza

10/30/2020, 2:21 PM
yes, I know the basic stuff. flowOf() and emit / collect
but this is a bit more envolved
l

louiscad

10/30/2020, 2:22 PM
Then you can play with simple flows and use
flatMapLatest
with them
You can hardcode a sample of what you could get from
dateSelectionAsFlow()
Then check all the blocks before joining them together once your understanding is sufficient
r

Remy Benza

10/30/2020, 2:23 PM
yeah, I know I must invest a bit more time to learn the API's better
but for today would love to fix this issue
aside from the flatMapLatest operator
doesn't my job.cancel() cancels the whole
Copy code
lifecycleScope.launch {
btw I'm on kotlinx coroutines 1.4
but on kotlin 1.3.61
l

louiscad

10/30/2020, 2:31 PM
If you use
flatMapLatest
, you should not need to do any manual coroutines cancelling for that purpose.
r

Remy Benza

10/30/2020, 2:58 PM
.flatMapLatest does indeed work on a codebase with kotlin 1.4.10
but im not seeing how a flow of date selection will retrigger my placedtracks flow in my viewmodel when all im doing is sending the epoch Long to the viewmodel and setting it in a LiveData value for the UI
shouldn't I manually cancel the current flow via it's job
l

louiscad

10/30/2020, 4:17 PM
flatMapLatest cancels the flow returned in the previous lambda automatically, that's what its doc specifies
6 Views