https://kotlinlang.org logo
Title
k

kupris

05/20/2019, 12:28 PM
Hello, I would like to use coroutines to handle delay and update of text from an EditText via his TextWatcher listener. In fact, if user continue to type something, I would like to cancel current coroutine and fire another one. It would works like timer/handler implementation for this type of case. Thanks in advance for your time and sorry for my crappy english 😅
g

gildor

05/20/2019, 12:48 PM
It's possible to do, do you have any particular problem with it?
k

kupris

05/20/2019, 12:50 PM
I don't know how properly cancel my job and then reuse my fragment's scope to launch another one.
g

gildor

05/20/2019, 12:54 PM
launch coroutine using async/await, cache Job and cancel it when you want
No need to cancel whole scope, only particular coroutine job
k

kupris

05/20/2019, 12:57 PM
Oh, ok, I develop the habit to use the job linked to my global scope 😅 Thanks for your time.
g

gildor

05/20/2019, 12:57 PM
But in general I'm not sure that your approach is good, probably better would be convert this callback to stream of events using Flow and write operator (if you need something like debounce/delay such operators already a available)
k

kupris

05/20/2019, 12:59 PM
I will have to read some documentation so, thanks again.
g

gildor

05/20/2019, 1:03 PM
Debounce operator already available in the latest version of kotlinx.coroutines for Flow
But I'm still not sure that you need debounce particular, depends on your case, but in general it better to use Flow as abstraction
This article do not use any coroutine stream abstraction, so I wouldn't recommend to use it
k

kupris

05/20/2019, 1:08 PM
I start to read about Flow, it's totally stranger to me
Thanks again for your advices
🙂
g

gildor

05/20/2019, 1:11 PM
k

kupris

05/20/2019, 1:14 PM
Thanks