Challenge - 5 Problems
Coroutine Networking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens when you launch a coroutine on the Main dispatcher?
Consider this Kotlin coroutine code snippet in an Android app:
What is the expected behavior of this code?
GlobalScope.launch(Dispatchers.Main) {
val data = fetchDataFromNetwork()
updateUI(data)
}What is the expected behavior of this code?
Android Kotlin
GlobalScope.launch(Dispatchers.Main) {
val data = fetchDataFromNetwork()
updateUI(data)
}Attempts:
2 left
💡 Hint
Remember that Dispatchers.Main runs coroutine code on the UI thread but suspend functions can switch threads internally.
✗ Incorrect
Using Dispatchers.Main launches the coroutine on the UI thread. However, suspend functions like fetchDataFromNetwork() typically switch to a background thread internally, so the UI stays responsive. After the network call completes, updateUI runs on the main thread safely.
❓ lifecycle
intermediate2:00remaining
What is the risk of using GlobalScope for network calls in Android?
In Android, what problem can occur if you launch network coroutines using GlobalScope.launch without tying them to a lifecycle?
Attempts:
2 left
💡 Hint
Think about what happens when the user leaves the screen while a coroutine is still running.
✗ Incorrect
GlobalScope coroutines live as long as the app process. If the activity is destroyed, the coroutine keeps running and may try to update a non-existent UI, causing crashes or memory leaks.
📝 Syntax
advanced2:00remaining
Which code snippet correctly performs a network call asynchronously using coroutines?
Choose the Kotlin code that correctly fetches data from a network on a background thread and updates the UI on the main thread.
Attempts:
2 left
💡 Hint
Network calls should run on IO dispatcher, UI updates on Main dispatcher.
✗ Incorrect
Option C runs fetchData() on the IO dispatcher (background thread), then switches to Main dispatcher to update UI safely. Option C runs fetchData() on Main thread, blocking UI. Option C runs fetchData() on Main thread inside withContext, blocking UI. Option C updates UI from IO thread, causing errors.
🔧 Debug
advanced2:00remaining
Why does this coroutine code cause a crash when updating UI?
Given this code:
Why does the app crash when updateUI is called?
GlobalScope.launch(Dispatchers.IO) {
val data = fetchData()
updateUI(data)
}Why does the app crash when updateUI is called?
Android Kotlin
GlobalScope.launch(Dispatchers.IO) {
val data = fetchData()
updateUI(data)
}Attempts:
2 left
💡 Hint
UI updates must happen on the main thread.
✗ Incorrect
Dispatchers.IO runs on a background thread. Calling updateUI from there causes Android to crash because UI changes must happen on the main thread.
🧠 Conceptual
expert2:00remaining
What is the main advantage of using suspend functions with coroutines for networking?
Why do suspend functions combined with coroutines improve networking code in Android apps?
Attempts:
2 left
💡 Hint
Think about how suspend functions let you write code that looks simple but runs asynchronously.
✗ Incorrect
Suspend functions let you write code that looks like normal sequential code but actually suspends without blocking threads. This makes asynchronous networking code easier to read and maintain.