https://kotlinlang.org logo
m

mcastiblanco

12/20/2017, 9:19 AM
I wanted to test what Android would do if a coroutine running in the main thread tries to do a network request (Android apps should crash when a network request is made on the UI thread). But I found out that nothing happens, nothing as in: - The app doesn't crash, but still - The code past the network request isn't executed anyways It would seem like the crash happened but was absorbed somewhere, and not even printed in the logs. What I did was to create a
suspend
function that calls a service, and then call that fun from
onCreate
using
Unconfined
so that the coroutine runes in
main
. relevant parts of the code:
Copy code
override fun onCreate(savedInstanceState: Bundle?) {
...
async(Unconfined) {
            fetchNpr()
        }
}

suspend fun fetchNpr() {
        Log.d("TEST", "Hello from [${Thread.currentThread().name}] thread")

        val factory = DocumentBuilderFactory.newInstance()
        val builder = factory.newDocumentBuilder()
        val xml = builder.parse("<https://www.npr.org/rss/rss.php?id=1001>")

        Log.d("TEST", "Encoding ${xml.xmlEncoding}")
    }
In the logs I see
Hello from [main] thread
, and then nothing. The app keeps running as if nothing but the second log is never printed. It doesn't seem like an expected behavior. I suppose the app should crash with
NetworkOnMainThreadException
. Should I create a bug for this in github?