🔄

Combine

iOS 13+

Apple's reactive programming framework for processing asynchronous events over time. Combine provides a declarative Swift API for handling values and events that change over time.

Networking
Official Apple Documentation →
iOS Version Compatibility
97.2%
coverage

of iOS users worldwide can use Combine (iOS 13+ required)

iOS VersionMarket ShareStatus
iOS 18.730.3%Supported
iOS 18.629.8%Supported
iOS 26.110.7%Supported
iOS 18.56.0%Supported
iOS 16.72.3%Supported
iOS 26.22.0%Supported
iOS 18.31.8%Supported
iOS 15.81.8%Supported
iOS 17.61.7%Supported
iOS 26.01.3%Supported

Key Features

Declarative reactive programming

Built-in operators for data transformation

Publisher and Subscriber protocols

Automatic memory management with AnyCancellable

Integration with SwiftUI and async/await

Thread safety and error handling

Code Examples

Basic Publisher

Create and subscribe to a simple publisher

import Combine

let publisher = [1, 2, 3, 4, 5].publisher

let cancellable = publisher
    .map { $0 * 2 }
    .filter { $0 > 5 }
    .sink { value in
        print("Received: \(value)")
    }
// Prints: Received: 6, 8, 10

Networking with Combine

Fetch data from an API using URLSession and Combine

import Combine
import Foundation

struct User: Codable {
    let id: Int
    let name: String
}

func fetchUser(id: Int) -> AnyPublisher<User, Error> {
    let url = URL(string: "https://api.example.com/users/\(id)")!
    
    return URLSession.shared
        .dataTaskPublisher(for: url)
        .map(\.data)
        .decode(type: User.self, decoder: JSONDecoder())
        .receive(on: DispatchQueue.main)
        .eraseToAnyPublisher()
}

// Usage
let cancellable = fetchUser(id: 1)
    .sink(
        receiveCompletion: { completion in
            if case .failure(let error) = completion {
                print("Error: \(error)")
            }
        },
        receiveValue: { user in
            print("User: \(user.name)")
        }
    )

Combine with @Published

Use @Published for reactive property updates

import Combine

class ViewModel: ObservableObject {
    @Published var searchText = ""
    @Published var results: [String] = []
    
    private var cancellables = Set<AnyCancellable>()
    
    init() {
        $searchText
            .debounce(for: .milliseconds(300), scheduler: DispatchQueue.main)
            .removeDuplicates()
            .sink { [weak self] text in
                self?.performSearch(text)
            }
            .store(in: &cancellables)
    }
    
    private func performSearch(_ text: String) {
        // Perform search with debounced text
        results = ["Result 1", "Result 2"]
    }
}

Frequently Asked Questions

What is Combine?

Apple's reactive programming framework for processing asynchronous events over time. Combine provides a declarative Swift API for handling values and events that change over time.

Which iOS versions support Combine?

Combine is available on iOS 13 and later. Currently, 97.2% of iOS users worldwide can use this framework.

What are alternatives to Combine?

Related frameworks include Swiftui, Urlsession. Each has different capabilities and iOS version requirements.