rx-java2

Handle null in RxJava2

亡梦爱人 提交于 2019-12-24 07:27:50
问题 There is a need where I want load data from local or remote. If local data fetched, remote data won't be fetched. As local data can be empty (i.e. null) , I have to handle null situation in RxJava2, so I do it with help of Optional util in java-8. Here is code. String data; Observable<Optional<String>> loadCacheData() { return Observable.create(emitter -> { emitter.onNext(Optional.ofNullable(null)); emitter.onComplete(); }); } Observable<Optional<String>> loadRemoteData() { Observable

Axon 4: EventSourcingHandler not triggered when applying event from a different thread

喜欢而已 提交于 2019-12-24 03:42:28
问题 I've encountered a little issue with command handling in Axon 4. Let say I have an aggregate that need to call an external service when handling a command. The external service uses an asynchronous client (vertx tcp client + rxjava), so the response is given in a different thread than the one that created the aggregate instance. I want to apply an event given the result of my service, but it does not work because the AggregateLifecycle.apply() call is on a different thread... How can I

How can I get the URL and method of RetroFit request on onSubscribe of RxJava 2 custom operator?

自作多情 提交于 2019-12-24 03:27:44
问题 So I'm trying to integrate Firebase performance for Http requests and add them manually as they show here (step 9). I'm using Retrofit 2 and RxJava 2, so I had the idea of doing a custom operator, check code below: Retrofit 2 Client @GET("branch-{environment}/v2/branches") fun getBranch(@Path("environment") environment: String, @Query("location") location: String, @Query("fulfilment_type") fulfilmentType: String): Single<Response<GetBranchResponse>> RxJava Call to the Retrofit Client private

RxJava. Read file to observable

那年仲夏 提交于 2019-12-24 01:31:48
问题 I am totally new to RxJava and reactive programming. I have an assignment where i must read file and store it to Observable. I have tried to make a Callable with BufferedReader inside and use Observable.fromCallable(), but it didn't work much. Could you please show me how can i do that? I am using RxJava 2.0. 回答1: A basic solution where I use a nested class FileObservableSource to produce the data and then defer the creation of the Observable until an Observer subscribes: import io.reactivex

Why are Flowables not Observables

痞子三分冷 提交于 2019-12-23 12:28:55
问题 Why are Flowables not Observables; Observable interface is pretty much a subset of Flowable, their implementations are pretty much the same. Why don't they implement a common interface so we can directly cast Flowable as Observable? 回答1: A small regret about introducing backpressure in RxJava 0.x is that instead of having a separate base reactive class, the Observable itself was retrofitted. The main issue with backpressure is that many hot sources, such as UI events, can't be reasonably

doOnSubscribe gets called on main thread

做~自己de王妃 提交于 2019-12-23 09:29:54
问题 After reading multiple blog posts and documentation, I came to the conclusion that following doOnSubscribe will be executed on a worker thread: Observable.just(1) .observeOn(Schedulers.io()) .doOnSubscribe(__ -> Log.d("Testing", "Testing")) // Shouldn't this be on worker thread? .subscribe(); But after debugging, I see doOnSubscribe is executed on main thread. I thought doOnSubscribe is similar to other operators and hence has similar threading behavior when coupled with subscribeOn and

rxJava buffer() with time that honours backpressure

流过昼夜 提交于 2019-12-23 03:21:26
问题 The versions of buffer operator that don't operate on time honour backpressure as per JavaDoc: http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#buffer-int- However, any version of buffer that involves time based buffers doesn't support backpressure, like this one http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#buffer-long-java.util.concurrent.TimeUnit-int- I understand this comes from the fact that once the time is ticking, you can't stop it similarly to,

RxJava2 get event when Observable/Completed is completed or disposed

扶醉桌前 提交于 2019-12-22 18:31:05
问题 I need to show a progress dialog when I subscribe to a Completable and hide it after the operation is completed(successfully or with an error) or is canceled. So I do final Completable completable = notificationRepository.markAllAsRead() .doOnSubscribe(d -> progressDialog.show()) .doOnError(error -> progressDialog.dismiss()) .doOnComplete(() -> progressDialog.dismiss()) .doOnDispose(() -> progressDialog.dismiss()); Is there any elegant way to get single callback when onError , onComplete or

Handling long running tasks with RxJava

℡╲_俬逩灬. 提交于 2019-12-22 17:58:46
问题 I'm trying to migrate an AsyncTask that sends messages to the server, to use RxJava. Roughly, the task does the following: 1) Creates a message that will be sent (persists to the database) 2) Shows the message to the user (state 'sending') 3) Sends the message to the server (code snippet below) 4) Marks the message as sent or failed (persists to the database) 5) Updates the UI I've created the required Rx chain which partially looks like this: public Observable<Message> sendMessage(Message

Convert/ wrap async listener to Observable (RxJava2)

倾然丶 夕夏残阳落幕 提交于 2019-12-22 09:45:50
问题 I want to wrap a real listener to Observable object. For starters here is a test case, with him everything is fine. @Override public void onCreate(@Nullable Bundle savedInstanceState) { getObservablePhoneState() // Run on a background thread .subscribeOn(Schedulers.io()) // Be notified on the main thread .observeOn(AndroidSchedulers.mainThread()) .subscribe(integer -> Log.i(TAG, "----- subscribe onNext = " + integer)); } private Flowable<Integer> getObservablePhoneState() { return Flowable