How can I detach from a sharedflow
# announcements
d
How can I detach from a sharedflow
z
#coroutines
d
Thanks
z
Do you want to just stop collecting from the flow, or stop the flow from collecting upstream (e.g. with
shareIn
)?
d
Just stop collecting myself
z
there are various operators that let you define how long to collect, but you can also cancel the collecting coroutine
d
That makes sense. So I would do something like create a new job, and cancel that job
z
with
launch
, yea that would work, if that fits your use case
d
But if I try doing something like
Copy code
val job = launch {
            cachedPosition.collect {
                if (cachedPosition != position) {
                    job.cancel()
                }
            }
        }
it doesn't work, because job isn't defined yet
So I'm putting the job in a instance variable, which feels hacky
e
just use
cancel()
, it's available on
CoroutineScope
(which
launch
provides)
☝️ 1
d
Thank you!