Publishers.SwitchToLatest
A publisher that flattens nested publishers.
Declaration
struct SwitchToLatest<P, Upstream> where P : Publisher, P == Upstream.Output, Upstream : Publisher, P.Failure == Upstream.FailureOverview
Given a publisher that publishes Publisher instances, the Publishers.SwitchToLatest publisher produces a sequence of events from only the most recent one. For example, given the type AnyPublisher<URLSession.DataTaskPublisher,NSError>, calling switchToLatest() results in the type SwitchToLatest<(Data, URLResponse), URLError>. The downstream subscriber sees a continuous stream of (Data, URLResponse) elements from what looks like a single URLSession.DataTaskPublisher even though the elements are coming from different upstream publishers.
When Publishers.SwitchToLatest receives a new publisher from the upstream publisher, it cancels its previous subscription. Use this feature to prevent earlier publishers from performing unnecessary work, such as creating network request publishers from frequently-updating user interface publishers.