Leave a rating/review
This is it! The final part of the course! :] After learning all the fun stuff about HTTP and Retrofit, you’ve reached the end of the course, where you’ll see how to extreme simplify the code for network requests.
This is all possible due to Kotlin Coroutines. Coroutines are quite an old concept, which uses the mechanism of suspending functions and resuming them later in time.
This way, you can have multiple things running at the same time, where you suspend one function, to finish another, and then go back to the first function.
Coroutines require two main things to be created - a coroutine builder which creates a coroutine depending on the configuration you give it, and a coroutine scope, which gives the coroutine some lifecycle to follow.
A builder is just another function, which can configure and create a coroutine, and then launch it. There are different builders.
- Launch is used to launch coroutines.
- WithContext is used to return a value from a coroutine.
- And async/await is used to wrap a call in a coroutine, and then expect a result from it later on.
A scope as the name states, is the scope of the app in which the coroutine can run. Once a scope finishes, the coroutine gets cancelled, and the code it’s running will stop. This is useful for optimizations, and stopping code from running after you leave an activity, or remove a fragment.
One of pieces of configuration you can use to change coroutines is a Dispatcher. A dispatcher is a mechanism which changes which thread or thread pool a coroutine will use. Using different dispatchers, you can change from the main thread, to a background thread, easily.
You’ll learn how to use coroutines for Retrofit here, but if you want to get the basic understanding of the fundamental concepts, on a simpler example, make sure to check out our Kotlin Coroutines: Fundamentals course.
In that course, using a small Android app, you’ll learn these concepts, and then coming back to this course, you’ll implement them once again, using Retrofit! :] So make sure to check out that course, or if you’ve already checked it out, feel free to continue to the next episode!