rx-java2

Why is doOnDispose not called?

天涯浪子 提交于 2020-05-11 05:24:39
问题 When creating an Observable like this: public void foo() { Observable observable = Observable.fromCallable(() -> { bar(); return ""; }) .doOnSubscribe(disposable -> System.out.println("onSubscribe")) .doOnDispose(() -> System.out.println("onDispose")); Disposable disposable = observable.subscribe(); disposable.dispose(); } private void bar() { System.out.println("bar"); } doOnSubcribe is called, doOnDispose is not called. Why is that? 回答1: You need to use the doFinally() operator. doOnDispose

RxJava Return single, execute completable after

六眼飞鱼酱① 提交于 2020-05-11 04:40:28
问题 I'm trying to accomplish the following: Return some data as single, execute a completable after. The following code does not compile due to single.andThen(). The actions need to be executed in this order. val single = Single.create<String> { it.onSuccess("result") } val completable = Completable.create { println("executing cleanup") } val together = single.andThen(completable) together.subscribe( { println(it) }, { println(it) } ) 回答1: Use flatMap : single.flatMap(v -> completable.andThen

RxJava flatMapIterable with a Single

这一生的挚爱 提交于 2020-05-10 03:42:25
问题 I'm trying to tidy up my code a little, and Single is looking like a good choice for me as I'm doing something that will only ever emit one result. I'm having an issue though as I was using flatMapIterable previously to take my response (a list) and do something on each item. I'm not seeing how I can achieve this with Single. getListOfItems() .flatMapIterable(items -> items) .flatMap(item -> doSomethingWithItem()) .toList() This works fine if getListOfItems is returning an Observable but if I

How can I replace inter thread communication using volatile variables with rxjava?

匆匆过客 提交于 2020-04-30 06:44:23
问题 I've got an application that does a lot of communication between threads by having one thread set a volatile variable on some object which another thread checks. I find this to be very error prone, and I want to try and replace it using RxJava bu there are some cases I can't figure out how to convert. The case I'm struggling with right now is where I have two threads, lets call one the controller and the other the measurer. The measurer's job is to record some quantity every 100ms. The

How to display Flowable data on a Textview in Room Database using Rxjava2

拜拜、爱过 提交于 2020-03-25 16:06:30
问题 DAO.class: @Dao public interface VisitorDAO { @Query("Select * from visitor") Flowable<List<Visitor>> getAll(); @Insert Completable Insert(Visitor visitor); //Using Single or Maybe tells the Database and the mainthread that this operation will be performed on Rxjava. @Update public void Update(Visitor visitor); @Delete public void Delete(Visitor visitor); } Code: @Override public void onComplete() { visitorFlowable = database.visitorDAO().getAll(); t.setText(visitorFlowable.); //is this the

Why is there no information output from the console after two seconds — Rxjava

青春壹個敷衍的年華 提交于 2020-03-16 07:25:09
问题 public class RxJavaTest { public static void main(String[] args) { Observable.timer(2, TimeUnit.SECONDS).subscribe(new Consumer<Long>() { @Override public void accept(Long aLong) throws Exception { System.out.println("timer: accept " + aLong); } }); } } Why is there no information output from the console after two seconds? 回答1: By default the timer operator is executed in a different Thread (in the computation thread pool) and your main thread is exited just after calling the subscribe and

How to store data in room database after fetching from server

∥☆過路亽.° 提交于 2020-03-14 04:57:30
问题 I am using Retrofit2 and Rxjava2 in my android app as a networking library and NodeJS and MongoDB as a backend service.I want to fetch data from server and store data in room database in order to if user open app again it fetches data from room and not from server until some new data is added on server. So far I have successfully fetched data from server and showing it in recycler view. What I want to achieve: 1) Store data in room database after fetching from server. 2) Show data from room

How to store data in room database after fetching from server

前提是你 提交于 2020-03-14 04:57:29
问题 I am using Retrofit2 and Rxjava2 in my android app as a networking library and NodeJS and MongoDB as a backend service.I want to fetch data from server and store data in room database in order to if user open app again it fetches data from room and not from server until some new data is added on server. So far I have successfully fetched data from server and showing it in recycler view. What I want to achieve: 1) Store data in room database after fetching from server. 2) Show data from room

Chaining method calls using RxJava

你说的曾经没有我的故事 提交于 2020-03-05 07:51:13
问题 Given two methods, both which returns a Single , what is the correct way, using Rx , to chain the two method calls together so that one method is called first, and the second once, and only if, the first completes successfully. Ideally, the second method will be able to access the value returned by the first. 回答1: Assuming your methods are like this: static Single<String> method1() { return Single.just("x"); } static Single<String> method2(String in) { return Single.just(in+"y"); } the

Mocking a http response using vert.x rxjava2 and Mockito

江枫思渺然 提交于 2020-02-28 23:18:31
问题 I'm playing with the rxjava vert.x (ver. 3.8.4) WebClient. My client will call some services so, by Mockito (ver. 3.2.4) I'm testing how it should handle an error. Here is how I mock the service's response: ... JsonObject jsonResponse = new JsonObject(); HttpResponse<Buffer> httpResponse = Mockito.mock(HttpResponse.class); Mockito.when(httpResponse.statusCode()).thenReturn(500); Mockito.when(httpResponse.bodyAsJsonObject()).thenReturn(jsonResponse); HttpRequest<Buffer> httpRequest = Mockito