Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
899 views
in Technique[技术] by (71.8m points)

swift - RxSwift merge different kind of Observables

How should I merge 2 different types of Observables in RxSwift?

For example:

var a: Observable<Int>
var b: Observable<Void>

Observable.of(a,b).merge() is not possible because of type parameter difference.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To merge them, they need to have the same type for their Element.

So, one option is to throw away their type information and cast to AnyObject. Now they can be merged:

let stringSubject = PublishSubject<String>()
let stringObservable = stringSubject.asObservable().map { $0 as AnyObject }

let intSubject = PublishSubject<Int>()
let intObservable = intSubject.asObservable().map { $0 as AnyObject }

Observable.of(stringObservable, intObservable).merge()
    .subscribeNext { print($0) }
    .addDisposableTo(disposeBag)

stringSubject.onNext("a")
stringSubject.onNext("b")
intSubject.onNext(1)
intSubject.onNext(2)
stringSubject.onNext("c")

Output:

a
b
1
2
c

Another option would be to wrap then in an enum:

enum Container {
    case S(String)
    case I(Int)
}

let stringSubject = PublishSubject<String>()
let stringObservable = stringSubject.asObservable().map { Container.S($0) }

let intSubject = PublishSubject<Int>()
let intObservable = intSubject.asObservable().map { Container.I($0) }

Observable.of(stringObservable, intObservable).merge()
    .subscribeNext { e in
        switch e {
        case .S(let str):
            print("next element is a STRING: (str)")
        case .I(let int):
            print("next element is an INT: (int)")
        }
    }
    .addDisposableTo(disposeBag)

stringSubject.onNext("a")
stringSubject.onNext("b")
intSubject.onNext(1)
intSubject.onNext(2)
stringSubject.onNext("c")

Output:

next element is a STRING: a
next element is a STRING: b
next element is an INT: 1
next element is an INT: 2
next element is a STRING: c

As for the other operators that can combine Observables of varying types (like zip and combineLatest), none work quite like merge. However, check those out. They might be better suited to your requirements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...