Please make sure you read this one, or the next six will be painful to read.
As the name suggests, merging observables is the merging of multiple Observables into one.
So without further ado. Let’s start
What is an operator?
Operators can help you create new sequences, or combine existing sequences to generate a new sequence. In the case we mentioned, we use several operators map, combineLatest, these two are the operators.
There are a lot of operators in RxSwift.
If you look at this picture, it makes your head feel big. There is a lot of content, but it can only be nibbled.
Today we are going to look at conversion operators.
merge
By using the Merge operator you can merge Observables into one Observables. When an Observable emits an element, it emits that element.
If an Observable issues an onError event, the merged Observable also issues it and immediately terminates the sequence.
Excerpt from :RxSwift Chinese documentation.
case
let disposeBag = DisposeBag(a)let subject1 = PublishSubject<String> ()let subject2 = PublishSubject<String> ()Observable.of(subject1, subject2)
.merge()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
subject1.onNext("🅰 ️")
subject1.onNext("🅱 ️")
subject2.onNext("1.")
subject2.onNext("2.")
subject1.onNext("🆎")
subject2.onNext("3."
Copy the code
Print the result
🅰️ 🅱️ ① ② ️ ③Copy the code
concat
Let two or more Observables be connected in sequence
The concat operator concatenates Observables in order so that the next Observables can emit elements only after the current one is sent.
Concat will wait for the first Observable to generate a complete event before subscribing to the next Observable. If the latter is a “hot” Observable, the resulting element will not be sent until the previous Observable produces the completion event.
StartWith is very similar. But startWith does not add elements after it, but inserts elements in front of it.
Merge is also very similar. Instead of connecting multiple Observables in sequence, merge them together without requiring Observables to emit elements in sequence.
Merge Concat must send onCompleted by the previous Observables before sending elements from the next Observables.
case
let disposeBag = DisposeBag()
let A = PublishSubject<String>()
let B = PublishSubject<String>()
Observable.of(A, B)
.concat()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
A.onNext("A-1")
A.onNext("A-2")
B.onNext("B-1")
A.onCompleted()
B.onNext("B-2")
A.onNext("A-3")
B.onNext("B-3")
Copy the code
Print the result
A-1
A-2
B-2
B-3
Copy the code
zip
Combine the elements of multiple Observables in a single function, and then emit the result of each combination. The zip operator combines multiple (up to eight) Observables elements in a single function and emits the result of that combination. It is composed strictly by the number of indexes in the sequence. For example, the first element of the returned Observable is composed of the first element of each source Observables. Its second element is composed of the second element of each source Observables. Its third element is combined from the third element of each source Observables, and so on. It has the same number of elements as the one with the fewest in the source Observables.
It’s kind of convoluted to look at this paragraph, but it’s faster to just look at the case
After testing, ZIP will strictly follow the one-to-one, two-to-two format.
case
let disposeBag = DisposeBag()
let first = PublishSubject<String>()
let second = PublishSubject<String>()
let third = PublishSubject<String>()
Observable.zip(first, second,third) { $0 + The $1 + $2}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
first.onNext("1")
second.onNext("A")
third.onNext("-test")
third.onNext("-test2")
third.onNext("-test3")
first.onNext("2")
second.onNext("B")
second.onNext("C")
second.onNext("D")
first.onNext("3")
first.onNext("4")
Copy the code
Print the result
1A-test
2B-test2
3C-test3
Copy the code
As you can see from the output, first and second both send 4 elements, while third sends only 3 elements, so only 3 are printed out. If the number of elements emitted by one Observables is not equal to that of the other Observables, the result that can be printed will be the least number of elements.
combineLatest
An element is emitted by any one of multiple Observables. This element is composed of the latest elements in these Observables by a function
The combineLatest operator combines the latest elements of multiple Observables in a single function and emits the result of that combination. Any one of these source Observables emits an element, and it emits an element (provided that the Observables have emitted elements before).
It’s easier to look at cases. case
let disposeBag = DisposeBag()
let first = PublishSubject<String>()
let second = PublishSubject<String>()
Observable.combineLatest(first, second) { $0 + The $1 }
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
first.onNext("1")
second.onNext("A")
first.onNext("2")
second.onNext("B")
second.onNext("C")
second.onNext("D")
first.onNext("3")
first.onNext("4")
Copy the code
Print the result
1A
2A
2B
2C
2D
3D
4D
Copy the code
CombineLatest is used to check whether the number of digits of the account and password exceed 5, when both checksums are true, then enable the login button.