Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

1.

Chapter
1.1. Combine is a declarative, reactive framework for processing asynchronous
events over time.
1.2. It aims to solve existing problems, like unifying tools for asynchronous
programming, dealing with mutable state and making error handling a starting
team player.
1.3. Combine revolves around three main types: publishers to emit events over
time, operators to asynchronously process and manipulate upstream events
and subscribers to consume the results and do something useful with them.
2. Chapter
2.1. Publishers transmit a sequence of values over time to one or more
subscribers, either synchronously or asynchronously.
2.2. A subscriber can subscribe to a publisher to receive values; however, the
subscriber’s input and failure types must match the publisher’s output and
failure types.
2.3. There are two built-in operators you can use to subscribe to publishers:
sink(_:_:) and assign(to:on:).
2.4. A subscriber may increase the demand for values each time it receives a
value, but it cannot decrease demand.
2.5. To free up resources and prevent unwanted side effects, cancel each
subscription when you’re done.
2.6. You can also store a subscription in an instance or collection of
AnyCancellable to receive automatic cancelation upon deinitialization.
2.7. A future can be used to receive a single value asynchronously at a later time.
2.8. Subjects are publishers that enable outside callers to send multiple values
asynchronously to subscribers, with or without a starting value.
2.9. Type erasure prevents callers from being able to access additional details of
the underlying type.
2.10. Use the print() operator to log all publishing events to the console and see
what’s going on.
3. Transforming Operators
3.1. collect() - The collect operator provides a convenient way to transform a
stream of individual values from a publisher into an array of those values.

3.2. map(_:) - Works just like Swift’s standard map.


3.3. map<T>(_:) or map<T0, T1>(_:_:) or map<T0, T1, T2>(_:_:_:) - map that
maps objects into two properties via key paths.
3.4. tryMap(_:) - Several operators, including map, have a counterpart try
operator that will take a closure that can throw an error.
3.5. flatMap(maxPublishers:_:) - The flatMap operator can be used to flatten
multiple upstream publishers into a single downstream publisher.

3.6. replaceNil(with:) - will receive optional values and replace nils with the value
you specify.
3.7. replaceEmpty(with:) - operator to replace — or really, insert — a value if a
publisher completes without emitting a value.

3.8. scan(_:_:) - It will provide the current value emitted by an upstream publisher
to a closure, along with the last value returned by that closure.
4. Filtering Operators
4.1. filter { $0.isMultiple(of: 3) } - consuming a publisher of values and
conditionally deciding which of them to pass to the consumer

4.2. removeDuplicates() - you have publishers that emit identical values in a row
that you might want to ignore
4.3. compactMap - you’ll want to perform some operation on your values that
might return nil, but who wants to handle all those nils
4.4. ignoreOutput() - Sometimes, all you want to know is that the publisher has
finished emitting values, disregarding the actual values.
4.5. first(where:) - It only takes as many values as it needs until it finds one
matching the predicate you provided

4.6. last(where:) - whose purpose is to find the last value matching a provided
predicate
4.7. dropFirst(3) - defaulting to 1 if omitted — and ignores the first count values
emitted by the publisher

4.8. drop(while:) - ignores any values emitted by the publisher until the first time
that predicate is met
4.9. drop(untilOutputFrom:) - It skips any values emitted by a publisher until a
second publisher starts emitting values, creating a relationship between them

4.10. prefix(_:) - take values until that condition is met


4.11. prefix(while:) - lets values from the upstream publisher through as long as the
result of that closure is true

4.12. prefix(untilOutputFrom:) - takes values until a second publisher emits


5. Combining operators
5.1. prepend(Output...) - This means it can take any number of elements, as long
as they’re of the same Output type as the original publisher

5.2. prepend(Sequence) - it takes any Sequence-conforming object as an input.


For example, it could take an Array or a Set
5.3. prepend(Publisher) - two different publishers and you want to glue their
elements together
5.4. append(Output...) - appends its items after the original publisher has
completed with a .finished event

5.5. append(Sequence) - appends its elements after all elements from the original
publisher have emitted
5.6. append(Publisher) - takes a Publisher and appends any elements emitted by
it to the end of the original publisher
5.7. switchToLatest - It lets you switch entire publisher chains on the fly while
canceling the pending publisher, thus switching to the latest one. You can
only use it on publishers that themselves emit publishers.

5.8. merge(with:) - “combining the emissions of different publishers”


5.9. combineLatest - “instead of interleaving the emissions of all publishers, it
emits a tuple with the latest values of all publishers whenever any of them
emit a value”
5.10. zip - emitting tuples of paired values in the same indexes
6. Time manipulation operators
6.1. delay(for:tolerance:scheduler:options) - operator time-shifts a whole
sequence of values: Every time the upstream publisher emits a value, delay
keeps it for a while then emits it after the delay you asked for, on the
Scheduler you specified

6.2. collect(.byTime(DispatchQueue.main, .seconds(5)))


6.3. collect(.byTimeOrCount(Context, Context.SchedulerTimeType.Stride, Int))

6.4. debounce(for: .seconds(1.0), scheduler: DispatchQueue.main) - waits for a


pause in values it receives, then emits the latest one after the specified
interval
6.5. throttle(for:scheduler:latest:) - waits for the specified interval, then emits
either the first or the latest of the values it received during that interval. It
doesn’t care about pauses
6.6. timeout(_:scheduler:options:customError:) - when a timeout operator fires, it
either completes the publisher or emits an error you specify. In both cases,
the publisher terminates
6.7. measureInterval(using:) - will emit measurements on the scheduler you
specify
7. Sequence operators
7.1. min() and min(by:) - lets you find the minimum value emitted by a publisher

7.2. max() and max(by:) - finds the maximum value emitted by a publisher
7.3. first() and first(where:) - will cancel the subscription when it receives the first
value emitted

7.4. last() and last(where:) - emits the last value that the publisher emits. This
means it's also greedy and must wait for the upstream publisher to finish
7.5. output(at:) - emits only the value emitted at the specified index

7.6. output(in:) - emits values whose indices are within a provided range
7.7. count() - emit a single number depicting how many values were emitted by
the upstream publisher
7.8. contains() and contains(where:) - emit true and cancel the subscription if the
specified value is emitted by the upstream publisher
7.9. allSatisfy() - emits a Boolean indicating whether all values emitted by the
upstream publisher match that predicate

7.10. reduce(_:_) and reduce(into:_:) - iteratively accumulate a new value based


on the emissions of the upstream publisher

You might also like