This is Understanding Combine, written by Matt Neuburg. Corrections and suggestions are greatly appreciated (you can comment here). So are donations; please consider keeping me going by funding this work at http://www.paypal.me/mattneub. Or buy my books: the current (and final) editions are iOS 15 Programming Fundamentals with Swift and Programming iOS 14. Thank you!


Timeout

.timeout (Publishers.Timeout) is sort of the opposite of .debounce: you supply a time interval, and the operator wants each successive value to arrive within that time interval. If it doesn’t, and no .finished completion has been received, the operator terminates the pipeline.

The parameters are (see .delay for more information about the first three):

_:
The time interval that must not elapse after a value arriving without another value arriving from upstream.
scheduler:
The scheduler on which the time interval will be measured.
options:
Optional. You’ll usually omit it.
customError:
A function that produces an Error. Optional; if omitted, the operator responds to a timeout by sending a .finished completion.

If you want to produce an Error, it needs to be of the same type as the upstream pipeline. For example, if the upstream pipeline’s Failure type is Never, you can’t supply an error function; to fix that, you’ll use .setFailureType before this operator:

.setFailureType(to: Error.self)
.timeout(0.1, scheduler:DispatchQueue.main) {MyError.oops}

Table of Contents