rx-java2

Unit testing Rxjava observables that have a delay

蹲街弑〆低调 提交于 2019-12-04 18:14:21
问题 I want to be able to unit test an Observable that has a delayed emission, but without actually waiting for the delay time. Is there a way to do this? I'm currently using a CountDownHatch to delay the assert, and that works fine, but increases the test run time. Example: val myObservable: PublishSubject<Boolean> = PublishSubject.create<Boolean>() fun myObservable(): Observable<Boolean> = myObservable.delay(3, TimeUnit.SECONDS) @Test fun testMyObservable() { val testObservable = myObservable()

How to handle dispose in RxJava without InterruptedException

两盒软妹~` 提交于 2019-12-04 18:08:21
In the below code snipped when dispose() is called, then the emitter thread is interrupted ( InterruptedException is thrown out of sleep method). Observable<Integer> obs = Observable.create(emitter -> { for (int i = 0; i < 10; i++) { if (emitter.isDisposed()) { System.out.println("> exiting."); emitter.onComplete(); return; } emitter.onNext(i); System.out.println("> calculation = " + i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } emitter.onComplete(); }); Disposable disposable = obs .subscribeOn(Schedulers.computation()) .subscribe(System.out::println

What's the difference between curly braces and normal brackets in RxJava with Kotlin

天涯浪子 提交于 2019-12-04 16:45:36
问题 I don't understand the real difference between the curly braces and and the normal brackets in Kotlin when using RxJava. For example, I have the following code which works as expected: someMethodThatReturnsCompletable() .andThen(anotherMethodThatReturnsACompletable()) .subscribe(...) But the following does NOT work: someMethodThatReturnsCompletable() .andThen { anotherMethodThatReturnsACompletable() } .subscribe(...) Note the difference in the andThen() part of the chain with the curly braces

Using Realm with RxJava 2

一曲冷凌霜 提交于 2019-12-04 16:27:32
I am using RxJava 2 in my Android application, and am integrating Realm. As far as I can tell, Realm only supports RxJava 1 by default, and allows an Observable to be returned when querying for RealmResults<?> , like so: Realm.getDefaultInstance() .where(VideoBundle.class) .findAll() .asObservable() .first() The Observable returned is from RxJava 1. How can I use Realm and RxJava 2 together? I have come across 2 relevant issues, found here and here , but no succinct answer was found. Additionally, the documentation (found here: https://realm.io/docs/java/latest/#rxjava ) mentions creating a

Run void method in background

久未见 提交于 2019-12-04 16:14:55
问题 I want to run a method in background using rxjava. I don't care about the result. void myHeavyMethod() { (...) } So far the only solution I have is to modify the return type to e.g. boolean . boolean myHeavyMethod() { (...) return true; } Afterwards I run: Completable.defer(() -> Completable.fromCallable(this::myHeavyMethod)) .subscribeOn(Schedulers.computation()) .subscribe( () -> {}, throwable -> Log.e(TAG, throwable.getMessage(), throwable) ); Is there a way to do it keeping the void

How to convert rxJava2's Observable to Completable?

試著忘記壹切 提交于 2019-12-04 15:33:41
问题 I have Observable stream, and I want to convert it to Completable, how I could do that? 回答1: The fluent way is to use Observable.ignoreElements() . Observable.just(1, 2, 3) .ignoreElements() Convert it back via toObservable if needed. 回答2: You can do something like below. Observable<Integer> observable = Observable.just(1, 2, 3); Completable completable = Completable.fromObservable(observable); Like on an Observable, you will have to subscribe to the completable to start the asynchronous

Emit each item from Flowable Room ORM

淺唱寂寞╮ 提交于 2019-12-04 13:02:09
I have a list of items in the Room ORM which I would like to display in a Recycler view. The data is being added from the network to the db. The problem is I am getting every time the whole list emited from the Flowable and not each item. I have tried with .distinctUntilChanged with no difference. @Query("SELECT * FROM items") Flowable<List<Item>> getItems(); I have tried also to return only a single item which loads only the first one that is the db. You can use flatMap to get stream of items. itemDao.getItems().flatMap(list -> { Item[] items = new Item[list.size()]; list.toArray(items);

Retry a call with Retrofit 2 and RxJava2 after displaying a dialog

巧了我就是萌 提交于 2019-12-04 12:21:23
问题 I'm calling an API using Retrofit 2 and RxJava2. If a call fails, in some cases (e.g. no Internet connection), I want to display an error dialog to the user and let him retry. As I'm using RxJava, I was thinking of using .retryWhen(...) but I don't know how to do that as it needs to wait for the user to press the button on the dialog. At the moment I display the dialog but it retries before the user presses any button. Plus I would like the call to not be retried when the user presses 'cancel

RxJava - When and why to use Observable.share()

我的未来我决定 提交于 2019-12-04 10:52:52
问题 I've seen patters like this: Observable<String> nameChanges = nameDataSource.changes().share(); // One subscriber autoUnsubscribe(nameChanges.subscribe(() -> { ... })); // Another subscriber autoUnsubscribe(nameChanges.map(...).filter(...).subscribe(...)); // autoUnsubscribe is called when the UI is torn down My question is: Why is it necessary to call share() whenever I want to listen to the Observable in multiple places? Why is not share() the default behavior for all observables? It would

Using flatMap in RxJava 2.x

时光怂恿深爱的人放手 提交于 2019-12-04 09:03:06
I'm working with an API of my own and a i'm hoping to chain a few paginated results using RxJava. I use cursor based pagination. (imagine there are 50 users in this first request): { "data":{ "status":"ok", "total":988, //users total "has_next_page":true, "end_cursor":"AQAxd8QPGHum7LSDz8DnwIh7yHJDM22nEjd", "users":[{"id":"91273813", "username":"codergirl", "full_name":"Code Girl", "picture_url":"https://cdn.com/21603182_7904715668509949952_n.jpg", }, ... ] } } Right now, I'm getting the first 50 results like this, using retrofit: public class DataResponse { @SerializedName("end_cursor")