ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Follow publication

Why Your Network Calls on the Main Thread Aren’t Crashing your Android App

Dobri Kostadinov
ProAndroidDev
Published in
4 min readFeb 14, 2025

--

Introduction

1. The Traditional Rule: Never Block the Main Thread

2. Why Is Your App Not Crashing?

2.1 OkHttp, Retrofit, and Ktor Do Not Always Handle Threading Automatically

val response = api.getData().execute() // This will block the main thread
api.getData().enqueue(object : Callback<Response<MyData>> {
override fun onResponse(call: Call<Response<MyData>>, response: Response<MyData>) {
// Handle response
}
override fun onFailure(call: Call<Response<MyData>>, t: Throwable) {
// Handle failure
}
})
interface ApiService {
@GET("endpoint")
suspend fun getData(): Response<MyData>
}
lifecycleScope.launch {
val result = api.getData() // Suspends but does not block the UI thread
handleResult(result)
}
val client = HttpClient(CIO) // Uses non-blocking I/O
val response: HttpResponse = client.get("https://api.example.com/data")

2.2 How Ktor Can Crash Your App???

fun fetchData() {
runBlocking {
val response: HttpResponse = client.get("https://api.example.com/data")
println(response.bodyAsText())
}
}
fun fetchData() {
CoroutineScope(Dispatchers.IO).launch {
val response: HttpResponse = client.get("https://api.example.com/data")
println(response.bodyAsText())
}
}

3. The Right Way: Always Use Dispatchers.IO

val result = withContext(Dispatchers.IO) {
api.getData()
}

4. Key Takeaways

--

--

Published in ProAndroidDev

The latest posts from Android Professionals and Google Developer Experts.

Written by Dobri Kostadinov

15+ years in native Android dev (Java, Kotlin). Expert in developing beautiful android native apps.

Responses (1)