Challenge - 5 Problems
withContext Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What happens to the UI when using withContext(Dispatchers.IO)?
Consider this Kotlin coroutine snippet in an Android app:
What is the effect of using
GlobalScope.launch(Dispatchers.Main) {
val data = withContext(Dispatchers.IO) {
fetchDataFromNetwork()
}
updateUI(data)
}What is the effect of using
withContext(Dispatchers.IO) here?Android Kotlin
GlobalScope.launch(Dispatchers.Main) {
val data = withContext(Dispatchers.IO) {
fetchDataFromNetwork()
}
updateUI(data)
}Attempts:
2 left
💡 Hint
Think about which thread handles UI updates and which handles long-running tasks.
✗ Incorrect
Using withContext(Dispatchers.IO) switches the coroutine to a background thread for the network call, preventing UI blocking. After that, it switches back to the main thread to update the UI safely.
🧠 Conceptual
intermediate1:30remaining
What does withContext do in Kotlin coroutines?
Choose the best description of what
withContext does in Kotlin coroutines.Attempts:
2 left
💡 Hint
Think about how you run code on different threads inside a coroutine.
✗ Incorrect
withContext temporarily changes the coroutine's context (like thread or dispatcher) for the code block, then returns to the original context.❓ lifecycle
advanced2:00remaining
What happens if you call withContext(Dispatchers.Main) inside withContext(Dispatchers.IO)?
Given this nested coroutine code:
What is the behavior of this code?
withContext(Dispatchers.IO) {
// background work
withContext(Dispatchers.Main) {
updateUI()
}
}What is the behavior of this code?
Android Kotlin
withContext(Dispatchers.IO) {
// background work
withContext(Dispatchers.Main) {
updateUI()
}
}Attempts:
2 left
💡 Hint
Think about how withContext can be nested to switch threads multiple times.
✗ Incorrect
withContext can be nested to switch contexts multiple times. Here, it runs background work on IO, then switches to Main thread to update UI safely.
📝 Syntax
advanced1:30remaining
Identify the syntax error in this coroutine with withContext usage
What is wrong with this Kotlin code snippet?
launch(Dispatchers.Main) {
val result = withContext(Dispatchers.IO)
fetchData()
display(result)
}Android Kotlin
launch(Dispatchers.Main) {
val result = withContext(Dispatchers.IO)
fetchData()
display(result)
}Attempts:
2 left
💡 Hint
Check how withContext expects its code block syntax.
✗ Incorrect
withContext requires a lambda block enclosed in braces {}. Omitting them causes a syntax error.
🔧 Debug
expert2:30remaining
Why does this withContext code cause a crash?
Analyze this code:
Why might this code crash the app?
GlobalScope.launch(Dispatchers.IO) {
val data = fetchData()
withContext(Dispatchers.Main) {
updateUI(data)
}
}
fun updateUI(data: String) {
textView.text = data
}Why might this code crash the app?
Android Kotlin
GlobalScope.launch(Dispatchers.IO) {
val data = fetchData()
withContext(Dispatchers.Main) {
updateUI(data)
}
}
fun updateUI(data: String) {
textView.text = data
}Attempts:
2 left
💡 Hint
Check if UI elements are properly initialized before use.
✗ Incorrect
The crash is likely due to textView being null or not initialized when updateUI is called, not because of thread switching.