Using Record in Combine
Posted by scotteg on August 26, 2019
A Common Approach to Exploring Operators
The examples will use the following setup code:
enum SomeError: Swift.Error {
case test
}
When learning about operators in Combine, you may find yourself writing a lot of boilerplate code to create publishers and send values, in order to explore how a particular operator works.
For example, here is an example of how you might explore how the scan(_:_:)
operator works:
let publisher = PassthroughSubject<Int, SomeError>()
_ = publisher
.scan(0) { $0 + $1 }
.sink(
receiveCompletion: { print($0) },
receiveValue: { print($0) }
)
publisher.send(1)
publisher.send(2)
publisher.send(3)
publisher.send(completion: .failure(.test))
This will print:
1
3
6
failure(__lldb_expr_48.SomeError.test)
If you don’t need to observe when an error event is emitted, you can access the publisher
computed property on sequences.
However, this publisher will not be able to emit an error event — its error type is Never
.
For example:
_ = (1...3).publisher
.scan(0) { $0 + $1 }
.sink(
receiveCompletion: { print($0) },
receiveValue: { print($0) }
)
This will print:
1
3
6
finished
Use Record
to Streamline Exploring Operators
There is a lesser-known publisher type in Combine called Record
that lets you pre-specify the values and completion event that will be emitted.
The first example could be implemented using Record
as follows:
_ = Record(
output: Array(1...3),
completion: .failure(SomeError.test)
)
.scan(0) { $0 + $1 }
.sink(
receiveCompletion: { print($0) },
receiveValue: { print($0) }
)
This will print the exact same output:
1
3
6
failure(__lldb_expr_48.SomeError.test)
To Learn More
If you’d like to learn more about Combine, check out this book I co-authored:
Combine: Asynchronous Programming with Swift
It’s packed with coverage of Combine concepts, hands-on exercises in playgrounds, and complete iOS app projects. Everything you need to transform yourself from novice to expert with Combine — and have fun while doing it!
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
© 2020 Scott Gardner