0
0
iOS Swiftmobile~20 mins

MainActor for UI updates in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
MainActor Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you update UI without MainActor?
Consider this Swift async function updating a UILabel text without @MainActor. What is the likely outcome?
iOS Swift
func updateLabel() async {
  label.text = "Hello"
}
AThe label text updates but only after a delay of several seconds.
BThe app may crash or show UI glitches due to thread violation.
CThe UI updates smoothly without any issues.
DThe code will not compile because label.text must be updated on MainActor.
Attempts:
2 left
💡 Hint
UI updates must happen on the main thread to avoid problems.
📝 Syntax
intermediate
2:00remaining
Which function declaration correctly ensures UI updates on the main thread?
Select the correct Swift function declaration that guarantees UI updates run on the main thread using MainActor.
A
@MainActor func updateUI() async {
  label.text = "Hi"
}
B
func updateUI() @MainActor async {
  label.text = "Hi"
}
C
func updateUI() async @MainActor {
  label.text = "Hi"
}
D
func updateUI() async {
  label.text = "Hi"
}
Attempts:
2 left
💡 Hint
The @MainActor attribute goes before the func keyword or before the function name.
lifecycle
advanced
2:00remaining
Why use @MainActor on a ViewModel class for UI updates?
You have a ViewModel class that updates UI-bound properties. Why is marking the entire class with @MainActor beneficial?
AIt forces all methods and properties to run on the main thread, preventing UI thread bugs.
BIt makes the class run faster by using multiple threads.
CIt disables concurrency features to simplify code.
DIt automatically updates the UI without any code changes.
Attempts:
2 left
💡 Hint
UI updates must happen on the main thread to avoid crashes.
🔧 Debug
advanced
2:00remaining
Identify the cause of a crash in this async UI update code
This Swift code crashes sometimes. What is the cause?
iOS Swift
func fetchData() async {
  let data = await loadData()
  label.text = data
}

func loadData() async -> String {
  // Simulate network delay
  try? await Task.sleep(nanoseconds: 1_000_000_000)
  return "Loaded"
}
AThe label outlet is nil causing the crash.
BThe Task.sleep call throws an error causing the crash.
CThe await keyword is used incorrectly inside fetchData.
DUpdating label.text off the main thread causes the crash.
Attempts:
2 left
💡 Hint
UI updates must happen on the main thread even after async calls.
🧠 Conceptual
expert
2:00remaining
What does @MainActor guarantee in Swift concurrency?
Choose the best description of what @MainActor guarantees when applied to a function or class.
A@MainActor guarantees code runs on any thread but prioritizes the main thread.
B@MainActor guarantees that all code runs on the main thread synchronously.
C@MainActor guarantees that code runs on the main thread and serializes access to prevent data races.
D@MainActor guarantees code runs on a background thread to avoid blocking UI.
Attempts:
2 left
💡 Hint
Think about thread safety and UI updates.