Sitemap

Member-only story

Comparing Combine subscription and async iteration

7 min readMar 30, 2025

--

Press enter or click to view image in full size
Photo by Maria Ziegler on Unsplash

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:

  1. Publishes user input (e.g., a search query) via an @Published property.
  2. Debounces user input (so you don’t trigger a network request for every keystroke).
  3. Calls an async function to fetch results from a remote endpoint.
  4. Updates another @Published property (the results) 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…

--

--

abdul ahad
abdul ahad

Written by abdul ahad

A software developer dreaming to reach the top and also passionate about sports and language learning