How do I cancel a Flow collection and start a whol...
# coroutines
r
How do I cancel a Flow collection and start a whole new flow?
g
You can format code blocks with triple ticks
r
👍 got an idea for my question? 😄
😂 1
l
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
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
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
so this job I would cancel?
when the user select a date via the date picker?
l
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
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
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
yes thnx @louiscad, I'm quite new to flow as you may have noticed 😋
l
Docs are often a great place to start when they're available, as is the case with first party Kotlin libraries 😉
r
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
👍 1
r
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
@Remy Benza Which Kotlin version are you using?
r
Copy code
'1.3.61'
l
With Kotlin 1.4.10, a bunch of type inference bugs have been fixed.
r
can't switch to 1.4 due to very large codebase and kotlinx serialization 0.14 migration work
l
kotlinx what?
kotlinx is a not a library
1.4 needs 1.0 for, so we can't just switch over
l
This library has been updated as well, and there's a migration guide
r
I know, the customer has other priorities now so..
l
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
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
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
haha
I'll send them your bill
l
Haha, I don't believe it 🙃 Do explicit types work?
r
but flatMap latest operator should be on the
Copy code
dateSelectionAsFlow()
right? and the canceling the job there is also the right place?
l
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
yes, I know the basic stuff. flowOf() and emit / collect
but this is a bit more envolved
l
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
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
If you use
flatMapLatest
, you should not need to do any manual coroutines cancelling for that purpose.
r
.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
flatMapLatest cancels the flow returned in the previous lambda automatically, that's what its doc specifies