android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.44k stars 8.28k forks source link

[PagingWithNetworkSample] network request was executed in main thread #930

Closed y4n9b0 closed 3 years ago

y4n9b0 commented 3 years ago

redditApi.getTop() was executed in main thread, both in PagingSource and RemoteMediator.

Also same problem here

dlam commented 3 years ago

In the SO post you linked, it clearly states that the suspending call adapter for Retrofit will dispatch on background thread for you. If this were not the case, Android would immediately throw a NetworkOnMainThreadException crashing the app.

To be clear, in a suspending function that makes a suspending network call via retrofit, only the network call is made on the background thread.. so you might end up with:

suspend fun load(..): .. {
    doStuff() // This will usually run on main thread by default
    retrofitCall() // This is automatically dispatched on Dispatchers.IO
    doMoreStuff() // This will usually run on main thread by default
}

A network call on main thread will definitely not produce an ANR, it will immediately crash the app.