O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.

Rxjava2 custom operator

326 visualizações

Publicada em

Rxjava2 custom operator

Publicada em: Software
  • Seja o primeiro a comentar

Rxjava2 custom operator

  1. 1. Rxjava2 custom operator Android Taipei 2018/11@Piccollage
  2. 2. Example: get first required element val inputData = Observable.just( User(1, "White Jame"), User(2, "Blue Jame"), User(3, "Green Jame"), User(4, "White John"), User(5, "Green John") ) inputData.filter { it.name.contains("John") } .firstElement() .test() .assertValues(User(4, "White John"))
  3. 3. Downside about compose 1. Need to always add a key word “compose” 2. Can not convert between Observable, Single, Maybe, Completable...
  4. 4. But we have kotlin now!!
  5. 5. Solution : extension function fun <T> Observable<T>.first(predicate: (T) -> Boolean): Maybe<T> { return this.filter(predicate) .firstElement() }
  6. 6. Pairwise
  7. 7. filterObservable
  8. 8. fun <T> Observable<Observable<T>>.filterObservable(predicateObservable: (Observable<T>) -> Single<Boolean>): Observable<Observable<T>> { val predicates = this.flatMap { predicateObservable(it).toObservable() } return this.zipWith(predicates, BiFunction { original: Observable<T>, check: Boolean -> Pair(original, check) }) .filter { (_, predicate) -> predicate } .map { (observable, _) -> observable } }
  9. 9. What The …...
  10. 10. Obs<Obs<T>> Obs<Obs<T>> Obs<Boolean> Obs<Boolean>> Obs<Obs<T>> Obs<Boolean>> Obs<Obs<T>> Obs<Obs<T>> zip filter
  11. 11. Source code https://gist.github.com/hungyanbin/4cd67008d5f9b60bcb7c38dd93cdda3e

×