I've encountered an odd issue here(new to Kotlin and coroutines). I am modifying a class variable in the function getMovies. In the launch block I get a JSON which I then run through GSON and get a mutable list of movie info entries. You can see 2 longToasts from Anko which displays a Toast, but the first one displays 20 and the other one 0. Why is that? Android Studio doesn't throw any errors and I am referencing the same moviesList variable. I tried numerous blogs and instructions on the Internet, but couldn't find anything useful. Any help would be super appreciated!
class MainActivity : Activity() {
private var moviesList: MutableList<Movie> = mutableListOf()
fun getMovies() {
launch(UI){
val result = async(CommonPool){
getResponseJSON()
}.await()
moviesList = Gson().fromJson(result, MovieDBResponse::class.java).results
longToast(moviesList.size.toString())
}
longToast(moviesList.size.toString())
}
}
20? Maybe try to write to log instead to ensure the order.async-await, it's an anti-pattern. UsewithContext(Dispatchers.IO)instead.