In Swift's Combine framework, operators are used to process and transform the values emitted by publishers. Here are some of the key operators you can use with publishers in Combine:
Basic Operators
-
sink(receiveCompletion:receiveValue:)
: Subscribes to a publisher and provides closures to handle completion and value events.publisher.sink(receiveCompletion: { completion in // Handle completion }, receiveValue: { value in // Handle new value })
-
assign(to:on:)
: Assigns each value from a publisher to a property on an object.publisher.assign(to: \.property, on: object)
Transforming Operators
-
map(_:)
: Transforms all elements from the upstream publisher with a provided closure.publisher.map { value in // Transform value }
-
compactMap(_:)
: Similar tomap
, but removesnil
values.publisher.compactMap { value in // Transform value, return nil if value should be removed }
-
flatMap(maxPublishers:_: )
: Transforms elements into a new publisher.publisher.flatMap { value in // Return a new publisher based on the value }
Filtering Operators
-
filter(_:)
: Emits only those elements that pass a given predicate.publisher.filter { value in // Return true if value should be included }
-
removeDuplicates()
: Suppresses duplicate elements.publisher.removeDuplicates()
-
first(where:)
: Publishes the first element that satisfies the predicate, then finishes.publisher.first { value in // Return true if value satisfies the predicate }
Combining Operators
-
combineLatest(_:)
: Combines the latest value from the publisher with another publisher.publisher.combineLatest(otherPublisher)
-
merge(with:)
: Merges elements from multiple publishers into a single stream.publisher.merge(with: otherPublisher)
-
zip(_:)
: Combines elements from this publisher with those from another publisher.publisher.zip(otherPublisher)
Sequence Operators
-
collect(_:)
: Collects a specified number of elements, and then publishes them as an array.publisher.collect(3)
-
reduce(_: _:)
: Applies a closure that accumulates each element of a publisher.publisher.reduce(0) { accumulated, value in // Combine accumulated value and new value }
Timing Operators
-
debounce(for:scheduler:)
: Publishes elements only after a specified time interval elapses between events.publisher.debounce(for: .seconds(1), scheduler: RunLoop.main)
-
delay(for:tolerance:scheduler:)
: Delays delivery of all output to the downstream receiver by a specified amount of time.publisher.delay(for: .seconds(1), scheduler: RunLoop.main)
-
throttle(for:scheduler:latest:)
: Publishes either the most-recent or the first element received during the specified interval.publisher.throttle(for: .seconds(1), scheduler: RunLoop.main, latest: true)
Error Handling Operators
-
catch(_:)
: Replaces an error with another publisher.publisher.catch { error in // Return a new publisher to replace the error }
-
retry(_:)
: Attempts to recreate the subscription to the upstream publisher if an error occurs.publisher.retry(3)
These operators allow you to manipulate and control the flow of data through your Combine pipelines, providing a powerful way to handle asynchronous events in Swift.