Using coroutines in real life
Microbenchmarks are very funny and they give us an idea of the power of Kotlin coroutines, but they don't represent a real-case scenario.
Let's introduce our real-case scenario:
enum class Gender { MALE, FEMALE; companion object { fun valueOfIgnoreCase(name: String): Gender = valueOf(name.toUpperCase()) } } typealias UserId = Int data class User(val id: UserId, val firstName: String, val lastName: String, val gender: Gender) data class Fact(val id: Int, val value: String, val user: User? = null) interface UserService { fun getFact(id: UserId): Fact }
Our UserService
interface has just one method—getFact
will return a Chuck Norris-style fact about our user, identified by the user ID.
The implementation should check first on a local database for a user; if the user doesn't exist in the database, it should get it from the RandomUser API service, (https://randomuser.me/documentation), and then store for future use. Once the service has a user...