Skip to content

Commit 6717eab

Browse files
teamehfreak4pc
authored andcommitted
withLatestFrom note and playground example
1 parent 7de6cfa commit 6717eab

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

β€ŽRx.playground/Pages/Combining_Operators.xcplaygroundpage/Contents.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,36 @@ example("switchLatest") {
155155
}
156156
/*:
157157
> In this example, adding ⚾️ onto `subject1` after adding `subject2` to `subjectsSubject` has no effect, because only the most recent inner `Observable` sequence (`subject2`) will emit elements.
158+
159+
----
160+
## `withLatestFrom`
161+
Merges two observable sequences into one observable sequence by combining each element from the first source with the latest element from the second source, if any.
162+
*/
163+
example("withLatestFrom") {
164+
let disposeBag = DisposeBag()
165+
166+
let foodSubject = PublishSubject<String>()
167+
let drinksSubject = PublishSubject<String>()
168+
169+
foodSubject.asObservable()
170+
.withLatestFrom(drinksSubject) { "\($0) + \($1)" }
171+
.subscribe(onNext: { print($0) })
172+
.disposed(by: disposeBag)
173+
174+
foodSubject.onNext("πŸ₯—")
175+
176+
drinksSubject.onNext("β˜•οΈ")
177+
foodSubject.onNext("πŸ₯")
178+
179+
drinksSubject.onNext("🍷")
180+
foodSubject.onNext("πŸ”")
181+
182+
foodSubject.onNext("🍟")
183+
184+
drinksSubject.onNext("🍾")
185+
}
186+
/*:
187+
> In this example πŸ₯— is not printed because `drinksSubject` did not emit any values before πŸ₯— was received. The last drink (🍾) will be printed whenever `foodSubject` will emit another event.
158188
*/
159189

160190
//: [Next](@next) - [Table of Contents](Table_of_Contents)

β€ŽRxSwift/Observables/WithLatestFrom.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ extension ObservableType {
1212
Merges two observable sequences into one observable sequence by combining each element from self with the latest element from the second source, if any.
1313

1414
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
15+
- note: Elements emitted by self before the second source has emitted any values will be omitted.
1516

1617
- parameter second: Second observable source.
1718
- parameter resultSelector: Function to invoke for each element from the self combined with the latest element from the second source, if any.
@@ -25,6 +26,7 @@ extension ObservableType {
2526
Merges two observable sequences into one observable sequence by using latest element from the second sequence every time when `self` emits an element.
2627

2728
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
29+
- note: Elements emitted by self before the second source has emitted any values will be omitted.
2830

2931
- parameter second: Second observable source.
3032
- returns: An observable sequence containing the result of combining each element of the self with the latest element from the second source, if any, using the specified result selector function.

0 commit comments

Comments
Β (0)