Kotlin Coroutines: Fundamentals

Feb 14 2024 · Kotlin 1.9, Android 13, Android Studio Giraffe

Part 2: Deep Dive into Coroutines

11. Challenge: Implement a Coroutine Bound to Composable with Exception Handling

Episode complete

About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 10. Handle Exceptions in Coroutines

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 11. Challenge: Implement a Coroutine Bound to Composable with Exception Handling

Check out the Kotlin Coroutines: In Depth course, the Kotlin Coroutines By Tutorials book, and the rest of our tutorials on Kotlin Coroutines.

Transcript: 11. Challenge: Implement a Coroutine Bound to Composable with Exception Handling

You made it to the end of this course, way to go! :] The only thing left to do is to put all the knowledge you’ve gained to use in the challenge.

You’ll implement an API call using coroutines inside a composable and with error handling. Don’t worry about the UI and API itself. They are already implemented for you.

Open the starter project inside Android Studio and go the the ChallengeScreen.kt file.

It contains a simple composable with one button and the text in a column. On each button click you should get the tutorial name from the fake API and display it in that text composable.

On top of the there is a place for initialization of the mechanism you’ll use to communicate with the fake API.

Next, you have the mutable property, which holds the tutorial name. In case of failure assign null.

Inside the button click handler there is a place to implement the coroutine.

At bottom of the file there is a fake API returning the tutorial names.

It also fails sometimes. Just like a real API would do for example in case of network issues.

The fake API may also throw a fatal error. You should also handle them properly.

Use all the knowledge you’ve gained in this course to implement the logic. Good luck!

Now, let’s see how you did! Compare your solution with the one in the final project.

Look at the scope obtained by the rememberCoroutineScope() function from the Jetpack Compose library.

It will be cancelled when the composable is no longer on the screen. That prevents memory leaks.

The coroutine context consists of the IO dispatcher.

It is important since there is a network request.

The second element of the context is the exception handler which logs the errors to the LogCat.

It is only for fatal exceptions like OutOfMemoryError and just rethrows them after logging. The app crashes after such rethrows.

But, it is on purpose because the process may be in an unstable state after such exceptions.

In the click handler there is a coroutine started on the scope using launch builder.

Inside the coroutine we try to fetch the tutorial name from the API. If there is a success, we assign the result to the currentTutorial property.

In case of a network error we assign null to the property in the catch block. The exception is also logged to the LogCat.

Congratulations! You’re now prepared to use coroutines and background processing in Android, when it comes to databases, network calls and a lot more!

If you’re looking to get a deeper understanding of Coroutines, and background processing, stick to our Android & Kotlin learning path! :]

See you in the next courses! :]