@MainActor
class ViewModel {
var count = 0
func increment() {
count += 1
}
}
let vm = ViewModel()
Task {
await vm.increment()
print(vm.count)
}
What will be printed?
medium
A. Compilation error
B. 0
C. 1
D. Runtime crash
Solution
Step 1: Understand @MainActor on class
The class ViewModel is marked @MainActor, so its methods and properties are isolated to the main actor.
Step 2: Analyze the code
await vm.increment() correctly increments count. Accessing vm.count inside the Task closure is safe because it's running on the main actor context.
Final Answer:
1 -> Option C
Quick Check:
Accessing @MainActor properties within the actor context is allowed [OK]
Hint: @MainActor properties can be accessed safely within actor context without await [OK]
Common Mistakes:
Thinking it prints 1 and forgetting await on print(vm.count)
Expecting runtime crash instead of compile error
Believing count stays 0 due to async context
4. You have this Swift function:
func updateLabel() {
label.text = "Hello"
}
When called from a background thread, the app crashes. How can you fix it using MainActor?
medium
A. Add @MainActor before the function declaration
B. Call updateLabel() inside DispatchQueue.global()
C. Remove the function and update label directly
D. Wrap label update inside DispatchQueue.main.async without @MainActor
Solution
Step 1: Identify cause of crash
UI updates must happen on main thread; background thread causes crash.
Step 2: Use @MainActor to fix
Adding @MainActor before function ensures it runs on main thread, preventing crash.
Final Answer:
Add @MainActor before the function declaration -> Option A
Quick Check:
@MainActor fixes UI thread crash [OK]
Hint: Use @MainActor to run UI code on main thread automatically [OK]
Common Mistakes:
Using global queue instead of main queue
Updating UI directly from background thread
Ignoring thread safety causing crashes
5. You want to update a SwiftUI view's text label from a background task safely. Which approach correctly uses @MainActor and async/await to update the UI?
hard
A. Use DispatchQueue.global().async to update the label
B. Declare the update function with @MainActor and call it with await inside the background task
C. Call the update function directly from the background thread without @MainActor
D. Update the label inside the background task without any special handling
Solution
Step 1: Understand UI update rules
UI updates must happen on main thread to avoid crashes and glitches.
Step 2: Use @MainActor with async/await
Marking the update function with @MainActor ensures it runs on main thread. Calling it with await from background task switches context safely.
Step 3: Evaluate other options
Options A, B, and D update UI from background thread causing errors or crashes.
Final Answer:
Declare the update function with @MainActor and call it with await inside the background task -> Option B
Quick Check:
@MainActor + await = safe UI update [OK]
Hint: Use @MainActor function called with await for UI updates [OK]
Common Mistakes:
Updating UI directly from background thread
Ignoring await when calling @MainActor function
Using global queue for UI updates
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.