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

Allan Wang

10/16/2018, 6:04 PM
I just watched the coroutine talk about creating a service that takes in locations, fetches content for new locations, and then reuses contents if a location has already been fetched:

https://www.youtube.com/watch?v=a3agLJQ6vt8

Over there, it looks like if any content fetching fails, the entire service is cancelled. How do we make it so that failing a content fetch will only cancel requests based on that particular location? The docs look like any exception beyond
CancellationException
will cancel the parent no matter what, so do we have to wrap our code in a try catch? To be more specific, this is the problem I want to solve: I have a method that takes in an id and returns an auth object I have other methods that will take in an auth object and then output something else I want to create a running stream for the duration of my lifecycle that will take in ids and actions. If an auth does not exist (say in a map), I want to fetch it. Once an auth is fetched, I want to run any pending actions on that auth. If an auth is being fetched and another request is made requiring that auth, it should wait for the previous fetch rather than getting a new auth itself. This pattern is already shown in the talk using
Map<T, List<K>>
. If the auth fetch fails, I want to cancel any pending actions. If an action fails, or anything else outside of this scope, I want the
CoroutineScope
to stay alive, until it is cancelled by the lifecycle.
e

elizarov

10/16/2018, 6:31 PM
Do try/catch around the fetch attempt and process a failure in the way you need.
👍 1
3 Views