ahulyk
01/31/2019, 2:39 PMlaunch(IO) {
val result = repoRepository.getAllRepos()
withContext(Main) {
textField.text = result
}
}
Example 2:
launch(Main) {
val result = repoRepository.getAllRepos()
withContext(Main) {
textField.text = result
}
}
Question is why i'm not getting android.os.NetworkOnMainThreadException in second example?streetsofboston
01/31/2019, 2:41 PMgetAllRepose()
do the actual fetching from the network, or does it just block and waits for an other thread, that does the networking, to return the result.ahulyk
01/31/2019, 2:47 PMgetAllRepose()
do the actual fetching from the network:
class RepoRepositoryImpl @Inject constructor(
private val apiService: APIService
) : RepoRepository {
override suspend fun getAllRepos(orgName: String) : List<RepoDto> {
return apiService.getAllRepos(orgName).await()
}
}
streetsofboston
01/31/2019, 2:53 PMapiService.getAllRepos()
does all the work, maybe on a different thread.RepoRepositoryImpl.getAllRepos()
may just wait (await()) for that other thread to return the resultahulyk
01/31/2019, 2:56 PMinterface APIService {
@GET("/orgs/kotlin/repos")
fun getAllReposRx(): Single<List<RepoDto>>
@GET("/orgs/{org}/repos")
fun getAllRepos(@Path("org") orgName: String): Deferred<List<RepoDto>>
}
streetsofboston
01/31/2019, 3:02 PMahulyk
01/31/2019, 3:20 PMlouiscad
01/31/2019, 3:36 PMstreetsofboston
01/31/2019, 3:56 PMahulyk
01/31/2019, 4:07 PMgildor
02/01/2019, 6:47 AMUnder the hood OkHttpClient uses new Thread() for network request - that is for sureThis is not exactly correct, OkHttp uses own dispatcher with thread pool
ahulyk
02/01/2019, 9:01 AM