As iOS lead on an app full of Combine view models, do you keep Combine for new code or move to async/await, and where do the two meet?
answer
- what shape is the work
- one-shot result versus values over time
- who rebuilds debounce and switchToLatest
- Publisher.values, from iOS 15
- Future runs eagerly; Deferred delays it
basics
~20 sDecide by the shape of the work: one-shot operations read better as async/await, while time-based streams that lean on debounce, combineLatest or switchToLatest stay natural in Combine. They meet through Publisher.values (iOS 15) and a Deferred Future wrapping a Task.
solid answer
~50 sThere is no single answer, so I'd set a rule by the shape of the work. One-shot operations, a fare request, a decode, a file write, are clearer as `async` functions: errors are `try`/`catch`, cancellation follows the task, and there is no `AnyCancellable` to keep. Streams of values over time where Combine's operators do real work, `debounce` on an airport field, `combineLatest` for form validation, `switchToLatest` for latest-query-wins, stay in Combine unless the team will rebuild those operators by hand or adopt a separate algorithms package. The seams should be explicit and few: `publisher.values` (iOS 15) turns a publisher into an `AsyncSequence` for `for await`, and `Deferred { Future { promise in Task { ... } } }` turns an `async` call into a lazy one-shot publisher. Combine is not deprecated, so migrate at those seams feature by feature instead of rewriting working pipelines, and write the rule down for review.
code
swift · 29 linesimport Combine
final class FareWatchModel {
@Published var passengers = 1
}
// Combine into async/await (iOS 15): the loop ends only when the
// publisher finishes or the task is cancelled.
func logPassengerChanges(of model: FareWatchModel) async {
for await count in model.$passengers.values {
print("passengers:", count)
}
}
// async/await into Combine: Deferred makes the work start per subscriber,
// not when the publisher is built.
func farePublisher(
fetch: @escaping @Sendable () async throws -> Int
) -> AnyPublisher<Int, Error> {
Deferred {
Future { promise in
Task {
do { promise(.success(try await fetch())) }
catch { promise(.failure(error)) }
}
}
}
.eraseToAnyPublisher()
}go deeper
Recall that Combine is not deprecated, that async/await handles one-shot work well, and that values (iOS 15) lets a for await loop read a publisher.
Explain both bridges: values with its AsyncPublisher, and Future inside Deferred so the work starts per subscriber rather than when the publisher is created.
Spot the seam bugs: an eager Future, a values loop with no owning task, a busy loop missing @Published changes, and a thread change between sink and for await.
Own the team rule: which shapes of work use which model, where bridges are allowed, how both are tested, and when an existing pipeline is worth rewriting.
## What the decision is really about Combine shipped in iOS 13 and is not deprecated, so nothing forces a rewrite. The real question for a lead is which style new code uses, and what each choice costs the team: - **Two models of cancellation.** Combine cancels through `AnyCancellable`; Swift concurrency cancels through tasks. Mixing them carelessly gives leaks in one direction and work that never stops in the other. - **Two error models.** A Combine `Failure` ends a stream; a thrown error ends a function or a loop. - **Operator coverage.** Combine's time-based operators have no built-in equivalent in the standard library. - **Hiring and onboarding.** New iOS engineers often learn async/await first and meet Combine's operators later. ## Where async/await fits better - **One-shot work**: a request, a decode, reading or writing a file. An `async throws` function reads top to bottom and has no token to manage. - **Sequential steps**: authenticate, then fetch, then save, where Combine would need a chain of `flatMap`s. - **Code already isolated to the main actor**, where results can be assigned directly after an `await`. The language mechanics of `async`, `await` and `AsyncSequence` belong to Swift's concurrency material; here what matters is where they replace a publisher. ## Where Combine still earns its place - **Search-as-you-type**: `debounce`, `removeDuplicates`, then `map` plus `switchToLatest()`. - **Form validation**: `combineLatest` over several `@Published` fields. - **Fan-out to several subscribers** with `share()` or a subject. - **Existing, tested pipelines** whose behaviour would have to be re-proved after a rewrite. ## The two bridges | Direction | API | Availability | Trap | |---|---|---|---| | Publisher into `for await` | `values`, an `AsyncPublisher` (or `AsyncThrowingPublisher` when the publisher can fail) | iOS 15 | The loop ends only when the publisher finishes or the task is cancelled | | One-shot publisher into `await` | `Future`'s `value` property | iOS 15 | Works only on a `Future`, which publishes one element | | `async` call into a publisher | `Deferred { Future { promise in Task { ... } } }` | iOS 13 types | Without `Deferred`, the work starts when the `Future` is created | When the publisher can fail, `values` returns an `AsyncThrowingPublisher`, so the loop is `for try await` and the publisher's failure arrives as a thrown error in a `catch` block, which is where a sink's `receiveCompletion` logic moves. `Future` adds a narrower bridge of its own: its `value` property, also iOS 15, lets a caller `await` the single result directly, throwing when the future fails. Going the other way there is no built-in adapter from an `async` function to a publisher, which is why the `Deferred` and `Future` pair with a `Task` inside is the common pattern; the task's result fulfils the promise exactly once. ## Traps at the seam 1. **`Future` is eager and single-shot.** It runs its closure as soon as it is created, once, and gives every subscriber the same result. Built in an initializer, the request starts even if nobody subscribes, and `retry(_:)` on it re-subscribes to a result that is already decided. `Deferred` creates a new `Future` per subscription, so each subscriber, and each retry, runs the work again. 2. **`for await` over an `@Published` property never ends by itself.** That publisher never finishes, so the loop runs until its task is cancelled. Own the task and cancel it with the screen. 3. **A busy loop can miss values.** If the loop body awaits while the property is set several times, the loop can receive only the latest of those values when it asks for the next one. Put `buffer(size:prefetch:whenFull:)` before `values` when every value matters. 4. **Threading changes.** A sink runs where the upstream emits; a `for await` loop body runs on its task's executor, so a loop in a main-actor task resumes on the main actor. ## A rule a team can apply 1. New one-shot APIs are `async`; wrap them for Combine callers with `Deferred` and `Future` only where a pipeline needs them. 2. New time-based UI pipelines may use Combine; each one has a test for its timing. 3. Every bridge sits at a view-model boundary, never in the middle of a pipeline. 4. Rewrite an existing pipeline only when that feature is being changed anyway.
- Why wrap the Future in Deferred instead of returning the Future directly?A `Future` runs its closure as soon as it is created, once, and replays that result to every subscriber. Returned directly, the request starts before anyone subscribes, and `retry(_:)` re-subscribes to the same settled failure, so it never runs the work again. `Deferred` builds a new `Future` per subscription, so the work starts on subscribe and every retry or subscriber gets a fresh attempt.
- How do you stop a for await loop over a view model's $query.values?An `@Published` publisher never finishes, so the loop ends only on `break`, a thrown error in a throwing loop, or cancellation of the task running it. Store that task and cancel it when the screen goes away; in SwiftUI, the `.task` modifier does this for you when the view disappears.
- A teammate proposes a rule of no new Combine code at all. How would you respond?Agree for one-shot work, push back for time-based UI pipelines. Without Combine the team must rebuild `debounce`, `combineLatest` and latest-wins switching or adopt a separate algorithms package, and test them again. A rule by the shape of the work, with explicit bridges at view-model boundaries, keeps the benefit without forcing rewrites.
- Combine is deprecated, so every pipeline has to be rewritten now
- Future is lazy and starts its work when the first subscriber arrives
- A for await loop over an @Published property's values ends when the screen disappears
- Publisher.values works on every iOS version that has Combine
- The standard library gives async sequences debounce and combineLatest built in
Assumes the iOS 27.1 SDK with an iOS 17 deployment target; Publisher.values and Future.value are iOS 15, so both bridges need no availability check.