Skip to content

Instantly share code, notes, and snippets.

@KentarouKanno
Last active April 19, 2020 04:51
Show Gist options
  • Save KentarouKanno/1d9a464e1a0e02c6705352ee56e4034a to your computer and use it in GitHub Desktop.
Save KentarouKanno/1d9a464e1a0e02c6705352ee56e4034a to your computer and use it in GitHub Desktop.

combineLatest

let password = PublishSubject<String>()
let repeatedPassword = PublishSubject<String>()

_ = Observable.combineLatest(password, repeatedPassword) { "\($0), \($1)" }
    .subscribe(onNext: { print("onNext: ", $0) })

password.onNext("a")
password.onNext("ab")

repeatedPassword.onNext("A")
repeatedPassword.onNext("AB")
repeatedPassword.onNext("ABC")
onNext:  ab, A
onNext:  ab, AB
onNext:  ab, ABC

zip

let intSubject = PublishSubject<Int>()
let stringSubject = PublishSubject<String>()

_ = Observable.zip(intSubject, stringSubject) { "\($0) \($1)" }
    .subscribe(onNext: { print($0) })

intSubject.onNext(1)
intSubject.onNext(2)

stringSubject.onNext("A")
stringSubject.onNext("B")
stringSubject.onNext("C")
stringSubject.onNext("D")

intSubject.onNext(3)
intSubject.onNext(4)
1 A
2 B
3 C
4 D
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment