Tolriq
11/23/2018, 8:41 PMsuspend fun fetchCharacterData(): CharacterGenerator.CharacterData = {
val apiData = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { URL(CHARACTER_DATA_ENDPOINT).readText()}
CharacterGenerator.fromApiData(apiData)
}
to start on good bases.zak.taccardi
11/23/2018, 8:43 PMCharacterGenerator.fromApiData(apiData)
should be called on the default dispatcher thoughTolriq
11/23/2018, 8:44 PMMike Palarz
11/23/2018, 8:48 PMfetchCharacterData()
written correctly. I've tried what @Tolriq recommended, but I'm getting a ton of compiler errors, namely that a suspension function can only be called within a coroutine context...Tolriq
11/23/2018, 8:52 PMMike Palarz
11/23/2018, 8:55 PMMike Palarz
11/23/2018, 9:17 PMsuspend fun fetchCharacterData(): CharacterGenerator.CharacterData = withContext(Dispatchers.Default){
val apiData = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { URL(CHARACTER_DATA_ENDPOINT).readText() }
CharacterGenerator.fromApiData(apiData)
}
And then I call fetchCharacterData()
as follows:
generateButton.setOnClickListener {
launch {
characterData = fetchCharacterData()
displayCharacterData()
}
}
Not sure if either of you have any comments on this, but I believe this is along the lines of what both of you were recommending. Thanks again!