rx-java2

Filtering data using RXJava2 Flowable

六月ゝ 毕业季﹏ 提交于 2019-12-22 09:29:28
问题 I am using Room and RxJava and I would like to use the power of the second to filter data coming from the first. Let's say room is returning Users. Flowable<List<User> getUsers() Then I wanted to filter users by age > 18 for example, so I performed the following : userDao.getUsers() .flatMap(listUser -> Flowable.fromIterable(listUser).filter(user -> user.age > 18)) .toList() .toFlowable() Unfortunately this is not working. My guess is that toList() is never finishing since onTerminated is

Rewrite Java code in Kotlin using Function Reference occurs SAM types conflict

半世苍凉 提交于 2019-12-22 08:35:57
问题 I have an example Java code using method reference that I want to rewrite to Kotlin. Java version is using method reference, solution is short and clear. But on the other hand I cannot use method reference in Kotlin. The only version that I've managed to write is one presented below. It seems like Function3 { s: String, b: Boolean, i: Int -> combine(s, b, i) } could be written in cleaner way (if possible method reference would be perfect). I'm new to Kotlin so I'll be grateful for any clues.

PublishSubject with backpressure in RxJava 2.x

删除回忆录丶 提交于 2019-12-22 03:51:18
问题 I am currently choosing between RxJava 1.x or 2.x for my current project. I basically need a PublishSubject with a backpressure strategy onBackpressureLatest() . I want to choose RxJava 2.x, but i can't quite get my head around on how to apply a backpressure strategy to a PublishSubject , as it inherits from Observable and not from Flowable . Could you please tell me how to create a PublishSubject with a onBackpressureLatest() backpressure strategy in RxJava 2.x ? 回答1: In 2.x the backpressure

Filter list of objects in RxJava

孤者浪人 提交于 2019-12-22 03:50:27
问题 I am trying to filter the list on the basis of it's property. For example, Sensors class has a property isActive and I want to get all the objects with isActive as true but I am unable to do it. I tried different ways but I didn't find the solution. Can someone help me to do it? Here is my code: mCompositeDisposable.add( fcService.getStationList() .subscribeOn(Schedulers.io()) .flatMap( stations -> { return fcService.getSensorList(stations.get(0).getName().getOriginal()); }) .subscribe(this:

RxJava 2, what happen the resultSelector argument of flatmap?

耗尽温柔 提交于 2019-12-22 00:03:42
问题 In RxJava1 flatmap had a overloaded method that allowed you to retain source values and pass it down the stream. I've gained this knowledge from the following blog post https://medium.com/rxjava-tidbits/rxjava-tidbits-1-use-flatmap-and-retain-original-source-value-4ec6a2de52d4 However, moving to RxJava2, I cannot seem to find it. I checked the changes from Rx1 and Rx2 and it is not listed. I would like to know if it exists still but I am perhaps not looking in the right place. I am using a

RxJava2 filter List<Object>

て烟熏妆下的殇ゞ 提交于 2019-12-21 12:11:48
问题 I'm trying to filter a List with RxJava2 such that each item (object) in the list should pass a validation check and I get a resulting List with only items that pass that test. For instance if my Object had the following structure, class MyClassA { int value1; int value2; } I want to only get the list of items where the value2 is 10. I have an API call function that returns an Observable of List, i.e. Observable<List<MyClassA>> as follows, apiService.getListObservable() .subscribeOn

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>