Bird
0
0

Why does Swift enforce @MainActor isolation for UI code instead of allowing UI updates from any thread?

hard📝 Conceptual Q10 of 15
iOS Swift - Concurrency
Why does Swift enforce @MainActor isolation for UI code instead of allowing UI updates from any thread?
ATo improve app startup speed
BTo prevent race conditions and crashes from concurrent UI access
CTo reduce memory usage
DTo allow UI updates to run faster on background threads
Step-by-Step Solution
Solution:
  1. Step 1: Understand UI thread safety

    UI frameworks are not thread-safe; concurrent access causes bugs and crashes.
  2. Step 2: Role of @MainActor isolation

    @MainActor ensures only one thread (main) accesses UI code, preventing race conditions.
  3. Final Answer:

    To prevent race conditions and crashes from concurrent UI access -> Option B
  4. Quick Check:

    @MainActor prevents unsafe concurrent UI access = A [OK]
Quick Trick: UI code must run on main thread to avoid crashes [OK]
Common Mistakes:
  • Thinking @MainActor improves speed or memory
  • Believing UI can safely update from any thread
  • Confusing thread safety with performance optimization

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes