---
title: Publishers.SwitchToLatest
framework: combine
role: symbol
role_heading: Structure
path: combine/publishers/switchtolatest
---

# Publishers.SwitchToLatest

A publisher that flattens nested publishers.

## Declaration

```swift
struct SwitchToLatest<P, Upstream> where P : Publisher, P == Upstream.Output, Upstream : Publisher, P.Failure == Upstream.Failure
```

## Overview

Overview 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.

## Topics

### Creating a switch-to-latest Publisher

- [init(upstream:)](combine/publishers/switchtolatest/init(upstream:).md)

### Declaring supporting types

- [Publishers.SwitchToLatest.Output](combine/publishers/switchtolatest/output.md)
- [Publishers.SwitchToLatest.Failure](combine/publishers/switchtolatest/failure.md)

### Inspecting publisher properties

- [upstream](combine/publishers/switchtolatest/upstream.md)

## Relationships

### Conforms To

- [Publisher](combine/publisher.md)

## See Also

### Republishing elements by subscribing to new publishers

- [Publishers.FlatMap](combine/publishers/flatmap.md)
