Successfully reported this slideshow.
Your SlideShare is downloading. ×

Reactive x

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 32 Ad
Advertisement

More Related Content

Advertisement

Recently uploaded (20)

Advertisement

Reactive x

  1. 1. ReactiveX
  2. 2. ReactiveX • Problem: Verarbeiten von asynchronen Datensequenzen • Mittel: Erweitertes Observer-Pattern mit Operatoren
  3. 3. Observer-Pattern Subject Observer A Observer B Observer C subscribe change notify
  4. 4. Observable Operatoren
  5. 5. Observable • Drei Events statt einem • onNext • onCompleted • onError • Es gibt “Hot” und “Cold” Observables • Variante: Single - für Sequenzen mit einem Element
  6. 6. Observable Operatoren
  7. 7. onCompletedonNext Quelle Beobachter Map Filter
  8. 8. Operatoren • Erzeugen, kombinieren, transformieren, filtern • Ergebnis ist ein neues Observable → Operatoren können verkettet werden • Reihenfolge ist entscheidend
  9. 9. Just Observable .just(1, 2, 3, 4, 5)
 .subscribe(onNext = { print("$it ") }) Output: 1 2 3 4 5
  10. 10. Concat Observable .concat(Observable.just(1, 4, 5), Observable.just(2, 3))
 .subscribe(onNext = { print("$it ") }) Output: 1 4 5 2 3
  11. 11. Filter Observable .just(1, 2, 3, 4, 5, 6) .filter { it % 2 == 0 }
 .subscribe(onNext = { print("$it ") }) Output: 2 4 6
  12. 12. Distinct Observable .just(1, 1, 2, 2, 3, 4, 5) .distinct()
 .subscribe(onNext = { print("$it ") }) Output: 1 2 3 4 5
  13. 13. Map Observable .just(1, 2, 3, 4, 5) .map { it * 2 }
 .subscribe(onNext = { print("$it ") }) Output: 2 4 6 8 10
  14. 14. FlatMap Observable .just(listOf(1, 2, 3), listOf(4, 5)) .flatMap { Observable.fromIterable(it) }
 .subscribe(onNext = { print("$it ") }) Output: 1 2 3 4 5
  15. 15. ObserveOn & SubscribeOn Observable .fromCallable { longRunningOperation() } .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())
 .subscribe(onNext = { print("$it ") })
  16. 16. Beispiele
  17. 17. Preise für Topseller fun getPrices(): Single<List<Price> { val priceRequest = PriceRequest()
 .setMaterial(material)
 .setMaterialOption(materialOption)
 .setFormats(topsellers.formats)
 
 frameType?.let { priceRequest.setFrameType(it) }
 
 return dataLayer
 .getPrice(priceRequest)
 .subscribeOn(Schedulers.io())
 .toObservable()
 .flatMap { Observable.fromIterable(it) }
 .map { prices ->
 Price(sumByFloat { it.priceCurrent }, sumByFloat { it.priceOriginal })
 }
 .toList()
 .observeOn(AndroidSchedulers.mainThread()) }
  18. 18. App-API @GET("price") fun getPrice( @QueryMap options: Map<String, String>, @Query("formatTypes[]") formatTypes: List<String> ): Observable<ApiResponse<Prices>>
  19. 19. Texteingabe hexInput .textChanges() .map { it.toString() } .subscribe { if (it.startsWith("#")) { try { color = Color.parseColor(it) } catch (ignored: IllegalArgumentException) {} } else { hexInput.setText("#${hexInput.text}") hexInput.setSelection(1) } }
  20. 20. Dropbox Bildersuche private fun searchImages(): Observable<List<SearchMatch>> =
 Observable
 .merge(
 (0 until ImagePickerHelpers.supportedExtensions.size).map {
 searchImageType(ImagePickerHelpers.supportedExtensions[it])
 }
 )
 .flatMap { Observable.fromIterable(it) } .groupBy { val path = it.metadata.pathDisplay val directoryPath = path.substring(0, path.lastIndexOf("/")) val directoryName = directoryPath.substring(directoryPath.lastIndexOf("/") + 1) directoryName } private fun searchImageType(extension: String): Observable<List<SearchMatch>> =
 Observable.fromCallable {
 dropboxClient
 .files()
 .search("", extension)
 .matches
 }
  21. 21. Dropbox Bildersuche private fun searchImages(): Observable<List<SearchMatch>> =
 Observable
 .concatEager(
 (0 until ImagePickerHelpers.supportedExtensions.size).map {
 searchImageType(ImagePickerHelpers.supportedExtensions[it])
 }
 )
 .flatMap { Observable.fromIterable(it) } .groupBy { val path = it.metadata.pathDisplay val directoryPath = path.substring(0, path.lastIndexOf("/")) val directoryName = directoryPath.substring(directoryPath.lastIndexOf("/") + 1) directoryName } .take(100) private fun searchImageType(extension: String): Observable<List<SearchMatch>> =
 Observable.fromCallable {
 dropboxClient
 .files()
 .search("", extension)
 .matches
 }
  22. 22. Dropbox Bildersuche private fun searchImages(): Observable<List<SearchMatch>> =
 Observable
 .concatEager(
 (0 until ImagePickerHelpers.supportedExtensions.size).map {
 searchImageType(ImagePickerHelpers.supportedExtensions[it])
 }
 )
 .flatMap { Observable.fromIterable(it) } .distinct { it.metadata.name } .groupBy { val path = it.metadata.pathDisplay val directoryPath = path.substring(0, path.lastIndexOf("/")) val directoryName = directoryPath.substring(directoryPath.lastIndexOf("/") + 1) directoryName } private fun searchImageType(extension: String): Observable<List<SearchMatch>> =
 Observable.fromCallable {
 dropboxClient
 .files()
 .search("", extension)
 .matches
 }
  23. 23. Dropbox Bildersuche private fun searchImages(): Observable<List<SearchMatch>> =
 Observable
 .concatEager(
 (0 until ImagePickerHelpers.supportedExtensions.size).map {
 searchImageType(ImagePickerHelpers.supportedExtensions[it])
 }
 )
 .flatMap { Observable.fromIterable(it) } .filter { it.metadata.name.contains("myposter") } .groupBy { val path = it.metadata.pathDisplay val directoryPath = path.substring(0, path.lastIndexOf("/")) val directoryName = directoryPath.substring(directoryPath.lastIndexOf("/") + 1) directoryName } private fun searchImageType(extension: String): Observable<List<SearchMatch>> =
 Observable.fromCallable {
 dropboxClient
 .files()
 .search("", extension)
 .matches
 }
  24. 24. data class CollageModel( val layouts: ImmutableList<CollageLayout>, val selectedLayout: CollageLayout, val selectedFormatGroup: CollageFormatGroup, val tiles: ImmutableList<CollageTile>, val texts: ImmutableList<CollageText>, val rotations: Int, val outerMargin: Float, val innerMargin: Float, val backgroundColor: Int, ⋮ )
  25. 25. MVP + ReactiveX • Model-View-Presenter Pattern • Model: Repräsentation des Zustands • View: Darstellung des Zustands • Presenter: Zustand verändern und View updaten
  26. 26. Subjects • Sind Observer und Observable • Geben einen Teil der ursprünglichen Daten weiter • Varianten: Async, Behavior, Publish, Replay
  27. 27. BehaviorSubject
  28. 28. MVP + ReactiveX • Presenter hat ein BehaviorSubject für Model-Objekte • Bei Eingaben wird ein neues Model-Objekt erzeugt und das onNext-Event des Subjects ausgelöst • View beobachtet das Subject und updated das UI bei onNext-Events
  29. 29. reactivex.io

×