rx-java

Web Reactive Programming - What are the advantages from the HTTP client point of view?

孤人 提交于 2019-12-20 10:33:34
问题 Lets suppose these two scenarios of a controller that generates some random numbers with a delay: 1) Reactive Spring 5 reactive application: @GetMapping("/randomNumbers") public Flux<Double> getReactiveRandomNumbers() { return generateRandomNumbers(10, 500); } /** * Non-blocking randon number generator * @param amount - # of numbers to generate * @param delay - delay between each number generation in milliseconds * @return */ public Flux<Double> generateRandomNumbers(int amount, int delay){

Combining Firebase realtime data listener with RxJava

烈酒焚心 提交于 2019-12-20 09:59:32
问题 I am using Firebase in my app, along with RxJava. Firebase is capable of notify your app whenever something changed in the backend data (addition, removals, changes, ...). I am trying to combine the feature of Firebase with RxJava. The data I am listening for is called Leisure , and the Observable emits LeisureUpdate which contains a Leisure and the type of update (add, remove, moved, changed). Here is my method which allows to subscribe to this events. private Observable<LeisureUpdate>

Fallback Observable for RxJava

浪子不回头ぞ 提交于 2019-12-20 09:38:38
问题 I'm in search for a better way to achieve a simple Observable fallback system for empty results when using RxJava. The idea is that, if a local query for a set of data results in zero items, then a fallback query (could be a network call, or something else) should be made instead. Currently, my code consists of the following: Observable.create(new Observable.OnSubscribe<Object>() { @Override public void call(Subscriber<? super Object> subscriber) { List<Object> results = queryLocalDatabase();

Download progress with RxJava, OkHttp and Okio in Android

丶灬走出姿态 提交于 2019-12-20 08:49:38
问题 In our app I download an image file with this code. I need to show download progress( downloaded bytes in percentage ) on UI. How I can get download progress in this code? I searched for solution, but still can't manage to do it on my own. Observable<String> downloadObservable = Observable.create( sub -> { Request request = new Request.Builder() .url(media.getMediaUrl()) .build(); Response response = null; try { response = http_client.newCall(request).execute(); if (response.isSuccessful()) {

How to use CompositeDisposable of RxJava 2?

北城以北 提交于 2019-12-20 08:06:29
问题 In RxJava 1, there was CompositeSubscription, but that is not present in RxJava2, There is something CompositeDisposable in rxJava2. How do I use CompositeDisposable or Disposable in RxJava2? 回答1: private final CompositeDisposable disposables = new CompositeDisposable(); // adding an Observable to the disposable disposables.add(sampleObservable() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(new DisposableObserver<String>() { @Override public void

Difference between RxJava API and the Java 9 Flow API

只谈情不闲聊 提交于 2019-12-20 07:59:10
问题 It seems on every iteration of Java for the last few major releases, there are consistently new ways to manage concurrent tasks. In Java 9, we have the Flow API which resembles the Flowable API of RxJava but with Java 9 has a much simpler set of classes and interfaces. Java 9 Has a Flow.Publisher , Flow.Subscriber , Flow.Processor , Flow.Subscription , and SubmissionPublisher , and that's about it. RxJava Has whole packages of Flow API-like classes, i.e. io.reactivex.flowables , io.reactivex

Handling exceptions inside Observable.fromCallable() when subscription gets cleared

筅森魡賤 提交于 2019-12-20 06:10:42
问题 I have a situation where a long running process is wrapped in an Observable.fromCallable() . This process is an OkHttp call and, if terminated, will throw an IOException . If the observable is subscribed to, then the disposable is stored in a CompositeDisposable and the exception is handled as expected. However, my code will clear the CompositeDisposable in some cases, triggering the OkHttp thread termination with no error handling, causing the app to crash with an unhandled exception. Here's

Is RxJava a good fit for branching workflows?

泪湿孤枕 提交于 2019-12-20 05:09:38
问题 I am using RxJava to process some notifications that we pull from a queue. RxJava seemed to work fine with a simple workflow, now with new requirements coming in, the flow is growing in complexity with more branches (please see below picture as a reference) I tried to exemplify the flow with a small unit test: @Test public void test() { Observable.range(1, 100) .groupBy(n -> n % 3) .toMap(GroupedObservable::getKey) .flatMap(m1 -> { Observable<Integer> ones1 = m1.get(0); Observable<Integer>

How to process first n items and remaining one differently in a observable stream

南楼画角 提交于 2019-12-19 11:34:22
问题 for example, given a stream of a certain number (m) of numbers (m1, m2, m3, m4, m5, m6...), and apply a transformation (2 * i) to first n items (n can be less, equal or larger than m), apply another transformation (3 * i) to the remaining items. and return result : m1*2, m2*2, m3*3, m4*3, m5*3, m6*3... (assuming n=2 here). I was trying to use take(n) and skip(n) and then concatwith, but looks like take(n) will drop the remaining items in the sequence , and make skip(n) after that return

RxJava 2 equivalent to isUnsubscribed

眉间皱痕 提交于 2019-12-19 09:09:34
问题 I've been working through the examples in the book Reactive Programming with RxJava, which is targeted at version 1 not 2. An introduction to infinite streams has the following example (and notes there are better ways to deal with the concurrency): Observable<BigInteger> naturalNumbers = Observable.create(subscriber -> { Runnabler = () -> { BigInteger i = ZERO; while (!subscriber.isUnsubscribed()) { subscriber.onNext(i); i = i.add(ONE); } }; new Thread(r).start(); }); ... Subscription