Skip to content

Actor isolation

SwiftSonicClient is a Swift actor. Every API call is async and runs serialized on the actor — no shared mutable state escapes, no manual locking required.

  • A single SwiftSonicClient instance is safe to share across tasks, view models, or background workers.
  • Concurrent calls into the client are serialized in actor-mailbox order. The HTTP work itself runs on URLSession in parallel; the bookkeeping (config access, token derivation, observers) is what’s serialized.

A common reflex is to spin up one client per task to “avoid contention”. Don’t. The actor handles contention. Multiple clients sharing the same ServerConfiguration would just duplicate transport state.

Use one SwiftSonicClient per logical server connection.

Because all the public types in SwiftSonic are Sendable, you can hand response models off to other actors, structured concurrency tasks, or MainActor view code without ceremony.

TODO: confirm that all response models in 0.6.x are marked Sendable. Verify by spot-checking Sources/SwiftSonic/Models/.

Standard structured concurrency cancellation applies. If the parent task is cancelled, in-flight requests are cancelled by URLSession and the call throws CancellationError.