24 Kasım 2019 Pazar

RxJava Flowable Sınıfı

Giriş
Flowable ve Observable farklı şeyler. Açıklaması şöyle.
The difference between Observable and Flowable is that Flowable handles back-pressure (implementing the reactive streams protocol) while Observable does not. Flowable is better suited for large streams of data coming from a backpressure-supporting source (for example, a TCP connection) while Observable is better suited for handling “hot” observables for which backpressure cannot be applied (for example, GUI events).
collect metodu
Flowable bir başka veriyapısına döndürülür.
Örnek
Şöyle yaparız.
source.collect(
    HashMultimap::create, 
    (mmap, item) -> mmap.put(item.first, item.second)
)
onBackpressureBuffer metodu
Örnek
Şöyle yaparız
var stream = Stream.generate(Math::random);

//RxJava
Flowable.fromStream(stream)
  .onBackpressureBuffer(0); //Throw is producer overflows

//Project Reactor
Flux.fromStream(stream)
  .onBackpressureError(); //Throw is producer overflows
subscribe metodu
Örnek
Şöyle yaparız.
networkCall()
  .subscribe( 
    success -> { ... }, 
    error -> { doSomethingWhenErrorHappened() }
   );

private Flowable<String> netWorkCall() {
     return networkAvailable() ? Flowable.just(...) : Flowable.error(...);          
}


Hiç yorum yok:

Yorum Gönder