Member-only story
Comparing Combine subscription and async iteration
Below is a more in-depth exploration of how to call async functions within a Combine pipeline. We’ll dissect the challenges, delve deeper into how Combine’s operators work under the hood, and explore the nuances of using Future (or flatMap) to bridge Swift Concurrency and Combine.
1. Understanding the Problem Context
The Typical Use Case
You have a view model or service that:
- Publishes user input (e.g., a search query) via an
@Publishedproperty. - Debounces user input (so you don’t trigger a network request for every keystroke).
- Calls an async function to fetch results from a remote endpoint.
- Updates another
@Publishedproperty (theresults) with the fetched data.
Pseudocode might look like this:
class SearchService {
@Published var query: String = ""
@Published var results: [SearchResult] = []
private let network = Networking()
func setup() {
$query
.debounce(for: .seconds(0.3), scheduler: DispatchQueue.main)
.map { query in
// We want to do something like:
// try await network.getResults(forQuery: query)
}
.assign(to…




