July 2018
Intermediate to advanced
266 pages
7h 11m
English
Let's now refactor the code to use suspending functions. We start with the same data class:
data class Profile( val id: Long, val name: String, val age: Int)
But instead of the verbose names, we can have cleaner ones. And even more important, instead of the interface forcing the implementation to be an async function, we only care about it suspending and returning a Profile – we can now remove Deferred from the equation:
interface ProfileServiceRepository { suspend fun fetchByName(name: String) : Profile suspend fun fetchById(id: Long) : Profile}
The implementation can be easily converted, too. Right now it doesn't need to actually suspend; it's a mock after all:
class ProfileServiceClient : ProfileServiceRepository ...
Read now
Unlock full access