Challenge: Live Data


#1

I can’t figure out how to complete the first Challenge of Ch. 22 “Live Data”.
Could someone give me a hint?

Thanks.


#2

Same issue here. cant complete.

Regards,
Shane.
Letgo Kissanime


#3

Since the API call is asynchronous, there has to be a delay before the character info is shown. I made a “placeholder” character that has as the name “Generating character…” to tell the user something is happening.

// in CharacterGenerator.kt
fun placeHolderCharacter() = CharacterData("Generating Character...", "", "", "", "")

Now inside onCreate() when checking the savedInstanceState, if it returns null then fetch the API and show the placeholder

characterData = savedInstanceState?.characterData ?: let {
        fetchCharacterFromAPI()
        CharacterGenerator.placeHolderCharacter()
    }

Where fetchCharacterFromAPI() is the contents of the click listener from Listing 22.8.

This only performs the initial fetch when the bundle is null, so after doing the initial retrieval and the user rotates the device, the character is stored.


Seems incompatible with release version 1.0.1 of Kotlin coroutines library