Recall & Review
beginner
What is the purpose of the
@MainActor attribute in Swift?The
@MainActor attribute ensures that the annotated code runs on the main thread, which is important for updating the user interface safely and avoiding concurrency issues.Click to reveal answer
beginner
How does
@MainActor help with UI updates in Swift concurrency?It guarantees that UI updates happen on the main thread, preventing crashes or unexpected behavior caused by updating UI elements from background threads.
Click to reveal answer
beginner
What happens if you try to update UI elements from a background thread without using
@MainActor?Updating UI from a background thread can cause crashes or visual glitches because UI frameworks expect changes on the main thread only.
Click to reveal answer
beginner
How do you mark a Swift function to run on the main thread using concurrency features?
You add the
@MainActor attribute before the function declaration to ensure it runs on the main thread.Click to reveal answer
intermediate
Explain the difference between
@MainActor and manually dispatching to the main queue.@MainActor is a language-level feature that automatically manages execution on the main thread, making code cleaner and safer, while manually dispatching uses DispatchQueue.main.async and requires explicit calls.Click to reveal answer
What does the
@MainActor attribute guarantee in Swift concurrency?✗ Incorrect
@MainActor ensures the code runs on the main thread, which is essential for UI updates.Why should UI updates be done on the main thread?
✗ Incorrect
UI frameworks require updates on the main thread to work correctly and avoid crashes.
Which Swift attribute helps you run a function on the main thread automatically?
✗ Incorrect
@MainActor is the correct attribute to ensure main thread execution.What is a common alternative to
@MainActor for running code on the main thread?✗ Incorrect
Using
DispatchQueue.main.async manually dispatches code to the main thread.If a function is marked with
@MainActor, what happens when you call it from a background thread?✗ Incorrect
Swift automatically switches execution to the main thread for
@MainActor functions.Describe why
@MainActor is important for UI work in Swift concurrency.Think about how UI updates must happen safely.
You got /4 concepts.
Explain how
@MainActor compares to using DispatchQueue.main.async for UI updates.Consider ease of use and safety.
You got /4 concepts.