What are Schedulers?

A Scheduler in Combine determines where and when work happens.
It controls which thread code runs on, ensuring smooth performance and responsiveness.

Think of it like a delivery system:

  • A publisher prepares the package (data).
  • The scheduler decides the best route (thread) for delivery.
  • The subscriber receives the package on the expected thread (UI/main thread, background, etc.).

Schedulers manage threading and execution timing for Combine operations.

🔗 Official Docs: Apple Documentation


How Schedulers Work

  • Determines which thread work runs on (e.g., UI updates on the main thread, background tasks elsewhere).
  • Used to switch between threads for performance optimization.
  • Ensures smooth UI updates by keeping heavy work off the main thread.

Example: Running a Task on a Background Thread

import Combine
 
let publisher = Just("Processing Data")
    .subscribe(on: DispatchQueue.global(qos: .background)) // Work starts in the background
    .receive(on: DispatchQueue.main) // Results are delivered on the main thread
 
let cancellable = publisher
    .sink { print("📢 Received on main thread: \($0)") }
 

Common Schedulers

  • DispatchQueue.main → Runs on the main UI thread (for UI updates).
  • DispatchQueue.global(qos: .background) → Runs in the background (for heavy tasks).
  • RunLoop.main → For timing-sensitive operations (e.g., animations).
  • OperationQueue → Uses a queue of worker threads.
  • immediateScheduler → Runs work immediately (mostly for debugging).

Key Takeaways

  • Schedulers control where Combine operations run.
  • Use .subscribe(on:)to specify where work should start.
  • Use .receive(on:)to specify where results should be handled.
  • Helps keep UI responsive by running heavy work in the background.