Bird
0
0

What happens if you call a @MainActor function from a background thread without using await or switching context?

medium📝 Predict Output Q5 of 15
iOS Swift - Concurrency
What happens if you call a @MainActor function from a background thread without using await or switching context?
AThe function automatically switches to main thread
BThe function runs immediately on the background thread
CThe code will cause a runtime error
DThe function is ignored
Step-by-Step Solution
Solution:
  1. Step 1: Understand @MainActor for synchronous functions

    @MainActor-isolated synchronous functions can be called from any thread.
  2. Step 2: Runtime behavior

    The Swift runtime automatically hops to the main thread synchronously (may block the caller).
  3. Final Answer:

    The function automatically switches to main thread -> Option A
  4. Quick Check:

    Sync @MainActor call from background = auto switch = C [OK]
Quick Trick: @MainActor sync functions automatically switch to main thread [OK]
Common Mistakes:
  • Expecting a runtime error
  • Thinking it runs on the background thread
  • Believing the function call is ignored

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More iOS Swift Quizzes