8개까지 combine할 수 있다.
extension Infallible {
/**
Merges the specified observable sequences into one observable sequence by using the selector function whenever any of the observable sequences produces an element.
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
- parameter resultSelector: Function to invoke whenever any of the sources produces an element.
- returns: An observable sequence containing the result of combining elements of the sources using the specified result selector function.
*/
public static func combineLatest<I1, I2>(_ source1: I1, _ source2: I2, resultSelector: @escaping (I1.Element, I2.Element) throws -> Element) -> RxSwift.Infallible<Element> where I1 : RxSwift.InfallibleType, I2 : RxSwift.InfallibleType
}
/// combineLatest
let num1 = Observable<Int>
.interval(.milliseconds(100), scheduler: MainScheduler.instance)
.take(5)
let num2 = Observable<Int>
.interval(.milliseconds(200), scheduler: MainScheduler.instance)
.take(5)
num1.subscribe(onNext: { _ in }).disposed(by: disposeBag)
num1.subscribe(onNext: { _ in }).disposed(by: disposeBag)
let combined = Observable.combineLatest(num1, num2) { n1, n2 -> String in
print(n1, n2)
return "\(n1)\(n2)"
}
combined.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
1 0
10
2 0
20
3 0
30
3 1
31
4 1
41
4 2
42
4 3
43
4 4
44
*/
extension ObservableType {
/**
Merges two observable sequences into one observable sequence by combining each element from self with the latest element from the second source, if any.
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
- note: Elements emitted by self before the second source has emitted any values will be omitted.
- parameter second: Second observable source.
- parameter resultSelector: Function to invoke for each element from the self combined with the latest element from the second source, if any.
- 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.
*/
public func withLatestFrom<Source, ResultType>(_ second: Source, resultSelector: @escaping (Self.Element, Source.Element) throws -> ResultType) -> RxSwift.Observable<ResultType> where Source : RxSwift.ObservableConvertibleType
/**
Merges two observable sequences into one observable sequence by using latest element from the second sequence every time when `self` emits an element.
- seealso: [combineLatest operator on reactivex.io](http://reactivex.io/documentation/operators/combinelatest.html)
- note: Elements emitted by self before the second source has emitted any values will be omitted.
- parameter second: Second observable source.
- 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.
*/
public func withLatestFrom<Source>(_ second: Source) -> RxSwift.Observable<Source.Element> where Source : RxSwift.ObservableConvertibleType
}
/// withLatestFrom
let num1 = Observable<Int>
.interval(.milliseconds(100), scheduler: MainScheduler.instance)
.take(5)
let num2 = Observable<Int>
.interval(.milliseconds(200), scheduler: MainScheduler.instance)
.take(5)
num1.withLatestFrom(num2) { n1, n2 in
return "\(n1)\(n2)"
}
.subscribe(onNext: { num in
print(num)
})
.disposed(by: disposeBag)
/*
10
20
31
41
*/
/**
Merges elements from all observable sequences from collection into a single observable sequence.
- seealso: [merge operator on reactivex.io](http://reactivex.io/documentation/operators/merge.html)
- parameter sources: Collection of observable sequences to merge.
- returns: The observable sequence that merges the elements of the observable sequences.
*/
public static func merge<Collection>(_ sources: Collection) -> RxSwift.Infallible<Self.Element> where Collection : Collection, Collection.Element == RxSwift.Infallible<Self.Element>
/// merge
let num1 = Observable<Int>
.interval(.milliseconds(100), scheduler: MainScheduler.instance)
.take(5)
let num2 = Observable<Int>
.interval(.milliseconds(200), scheduler: MainScheduler.instance)
.take(5)
let merged = Observable.merge(num1, num2)
merged.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
0
1
0
2
3
1
4
2
3
4
*/
extension InfallibleType {
/**
Merges the specified observable sequences into one observable sequence by using the selector function whenever all of the observable sequences have produced an element at a corresponding index.
- seealso: [zip operator on reactivex.io](http://reactivex.io/documentation/operators/zip.html)
- parameter resultSelector: Function to invoke for each series of elements at corresponding indexes in the sources.
- returns: An observable sequence containing the result of combining elements of the sources using the specified result selector function.
*/
public static func zip<E1, E2>(_ source1: RxSwift.Infallible<E1>, _ source2: RxSwift.Infallible<E2>, resultSelector: @escaping (E1, E2) throws -> Self.Element) -> RxSwift.Infallible<Self.Element>
}
/// zip
let num1 = Observable<Int>
.interval(.milliseconds(100), scheduler: MainScheduler.instance)
.take(5)
let num2 = Observable<Int>
.interval(.milliseconds(200), scheduler: MainScheduler.instance)
.take(5)
let zip = Observable.zip(num1, num2)
zip.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
(0, 0)
(1, 1)
(2, 2)
(3, 3)
(4, 4)
*/
extension ObservableType {
/**
Concatenates the second observable sequence to `self` upon successful termination of `self`.
- seealso: [concat operator on reactivex.io](http://reactivex.io/documentation/operators/concat.html)
- parameter second: Second observable sequence.
- returns: An observable sequence that contains the elements of `self`, followed by those of the second sequence.
*/
public func concat<Source>(_ second: Source) -> RxSwift.Observable<Self.Element> where Source : RxSwift.ObservableConvertibleType, Self.Element == Source.Element
}
/// concat
let num1 = Observable<Int>
.interval(.milliseconds(100), scheduler: MainScheduler.instance)
.take(5)
let num2 = Observable<Int>
.interval(.milliseconds(200), scheduler: MainScheduler.instance)
.take(5)
let concat = Observable.concat(num1, num2)
concat.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
0
1
2
3
4
0
1
2
3
4
*/
extension InfallibleType {
/**
Prepends a value to an observable sequence.
- seealso: [startWith operator on reactivex.io](http://reactivex.io/documentation/operators/startwith.html)
- parameter element: Element to prepend to the specified sequence.
- returns: The source sequence prepended with the specified values.
*/
public func startWith(_ element: Self.Element) -> RxSwift.Infallible<Self.Element>
}
Observable<Int>.of(1,2,3,4,5)
.startWith(0)
.subscribe({
print($0)
})
.disposed(by: disposeBag)
/*
next(0)
next(1)
next(2)
next(3)
next(4)
next(5)
completed
*/
extension ObservableType where Self.Element : RxSwift.ObservableConvertibleType {
/**
Transforms an observable sequence of observable sequences into an observable sequence
producing values only from the most recent observable sequence.
Each time a new inner observable sequence is received, unsubscribe from the
previous inner observable sequence.
- seealso: [switch operator on reactivex.io](http://reactivex.io/documentation/operators/switch.html)
- returns: The observable sequence that at any point in time produces the elements of the most recent inner observable sequence that has been received.
*/
public func switchLatest() -> RxSwift.Observable<Self.Element.Element>
}
extension ObservableType {
/**
Propagates the observable sequence that reacts first.
- seealso: [amb operator on reactivex.io](http://reactivex.io/documentation/operators/amb.html)
- returns: An observable sequence that surfaces any of the given sequences, whichever reacted first.
*/
public static func amb<Sequence>(_ sequence: Sequence) -> RxSwift.Observable<Self.Element> where Sequence : Sequence, Sequence.Element == RxSwift.Observable<Self.Element>
}
let num1 = Observable<Int>
.interval(.milliseconds(100), scheduler: MainScheduler.instance)
.take(5)
let num2 = Observable<Int>
.interval(.milliseconds(200), scheduler: MainScheduler.instance)
.take(5)
num1.amb(num2)
.subscribe(onNext: {
print($0)
})
.disposed(by: disposeBag)
/*
0
1
2
3
4
*/