matt tighe
09/06/2018, 6:13 PMfun refreshData() {
networkStatus = Status.ACTIVE // class member
launch(CommonPool) {
asyncAwait {
remoteSource.fetchData().forEach {
roomDatabase.dao().insertData(it)
}
networkStatus = Status.INACTIVE
}
}
}
i’d like to be able to unit test that the status is updated while the coroutine is runningJonathan Walsh
09/06/2018, 6:21 PMDico
09/06/2018, 8:49 PMrunBlocking {
remoteSource.fetchData().forEach {
roomDatabase.dao().insertData(it)
}
networkStatus = Status.INACTIVE
}
launch {
// code here
}.join()
gildor
09/07/2018, 1:44 AMasyncAwait
? Shortcut for async{}.await()? If so, it doesn’t make any sense, there are not cases when you call async{} and await immidiately. Probably you can just call the function or use withContext insteadCommonPool
in probably redundantlaunch(CommonPool)
and asyncAwait
so it allows you to wait (suspend) until function finish and check side effect after
It’s exactly the same bad style as launch thread in a function: you just cannot test it and have other problems with lifecycle of such function, concurrent updates and so onwithContext(IO)
).
or use async API of Room (there are adapters for RxJava and LiveData)Dico
09/07/2018, 8:36 AM