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

ildar.i [Android]

07/02/2020, 10:56 AM
Got a question: - I have three
suspend
operations, each can be called independently:
foo1(list)
,
foo2(list)
and
foo3(list)
- each function changes state of items in list -
foo1
just makes request to the server and returns updated list (updates 1 step) -
foo2
calls
foo1
and then with combined result makes a request (updates 2 steps) -
foo3
calls
foo2
, combines result and makes a request (updates 3 steps) Problem is,
foo2
needs to gather data (SMS-code) via DialogFragment. Is there a way to build a continuous pipeline, so the
foo3
can continue its flow after
foo2
called a dialog, gathered input and returned?
e

elizarov

07/02/2020, 12:42 PM
Yes
👍 2
i

ildar.i [Android]

07/02/2020, 4:18 PM
that wasn't helpful😐
e

elizarov

07/02/2020, 4:45 PM
Sorry, I’m not an Android developer, so I don’t know the specific code you should write. I just know it is possible, because you can always call
suspendCancellableCoroutine { … }
and then resume the coroutine on whatever callback you receive when the dialog had gathered input and returned. Alternatively, you can create a
CompletableDeferred
, open the dialog, and then call
deferred.await()
in your suspending function, arranging it so that you call
deferred.complete(data)
when you have some
data
from your dialog to be processed in the subsequent steps.
i

ildar.i [Android]

07/02/2020, 4:54 PM
wow that's a lot of insight, thank you!
g

gildor

07/03/2020, 2:06 PM
suspendCancellableCoroutine works fine for this use case, we have a suspending abstraction for dialogs in out project
i

ildar.i [Android]

07/03/2020, 2:19 PM
How does it look like? I ended up with CompletableDeferred
5 Views