> For the complete documentation index, see [llms.txt](https://presto95.gitbook.io/combine-rxswift-reactiveswift/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://presto95.gitbook.io/combine-rxswift-reactiveswift/publisher/switchtolatest.md).

# Publishers.SwitchToLatest

**제네릭 구조체** | 중첩된 Publisher들을 평평하게 펴주는 Publisher

이니셜라이저는 한 개의 인자를 받는다.

* `upstream` : 상위에 흐르는 Publisher

예를 들어 `Publisher<Publisher<Data, NSError>, Never>` 타입의 Publisher에 해당 Publisher를 적용하면 `Publisher<Data, NSError>` 타입의 Publisher를 얻을 수 있다.

`switchToLatest` 오퍼레이터와 관련이 있다.

```swift
// 1 : Publishers.SwitchToLatest Publisher
Publishers.SwitchToLatest(upstream: Publishers.Sequence<[Just<Int>], Never>(sequence: [Just(1), Just(2), Just(3)]))
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine SwitchToLatest Error")
    case .finished:
      print("Combine SwitchToLatest Finish")
    }
  }, receiveValue: { value in
    print("Combine SwitchToLatest : \(value)")
  })
  .store(in: &cancellables)

// 2 : switchToLatest() Operator
Publishers.Sequence<[Just<Int>], Never>(sequence: [Just(1), Just(2), Just(3)])
  .switchToLatest()
  .sink(receiveCompletion: { completion in
    switch completion {
    case .failure:
      print("Combine SwitchToLatest Error")
    case .finished:
      print("Combine SwitchToLatest Finish")
    }
  }, receiveValue: { value in
    print("Combine SwitchToLatest : \(value)")
  })
  .store(in: &cancellables)

// Combine SwitchToLatest : 1
// Combine SwitchToLatest : 2
// Combine SwitchToLatest : 3
// Combine SwitchToLatest Finish
```

상위 Publisher는 `Just` Publisher를 발행하는 `Publishers.Sequence` Publisher이다. 이것에 `switchToLatest`를 적용하여 상위 Publisher를 평평하게 해주었다.

결과적으로 1, 2, 3의 값을 차례대로 내고 종료한다.

## RxSwift

Observable 변환 오퍼레이터 `flatMap`의 클로저에서 인자를 그대로 반환하여 해당 동작을 구현할 수 있다.

```swift
Observable.from([Observable.just(1), Observable.just(2), Observable.just(3)])
  .flatMap { $0 }
  .subscribe(onNext: { value in
    print("RxSwift SwitchToLatest : \(value)")
  }, onError: { _ in
    print("RxSwift SwitchToLatest Error")
  }, onCompleted: {
    print("RxSwift SwitchToLatest Finish")
  })
  .disposed(by: disposeBag)

// RxSwift SwitchToLatest : 1
// RxSwift SwitchToLatest : 2
// RxSwift SwitchToLatest : 3
// RxSwift SwitchToLatest Finish
```

## ReactiveSwift

`flatMap` 오퍼레이터의 클로저에서 인자를 그대로 반환하여 해당 동작을 구현할 수 있다.

```swift
SignalProducer([SignalProducer(value: 1), SignalProducer(value: 2), SignalProducer(value: 3)])
  .flatMap(.concat) { $0 }
  .start { event in
    switch event {
    case let .value(value):
      print("ReactiveSwift SwitchToLatest : \(value)")
    case .failed:
      print("ReactiveSwift SwitchToLatest Error")
    case .completed:
      print("ReactiveSwift SwitchToLatest Finish")
    default:
      break
    }
  }

// ReactiveSwift SwitchToLatest : 1
// ReactiveSwift SwitchToLatest : 2
// ReactiveSwift SwitchToLatest : 3
// ReactiveSwift SwitchToLatest Finish
```

## 참고

[ReactiveX - Operators - FlatMap](http://reactivex.io/documentation/operators/flatmap.html)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://presto95.gitbook.io/combine-rxswift-reactiveswift/publisher/switchtolatest.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
