rx-java2

RXJava2: correct pattern to chain retrofit requests

别说谁变了你拦得住时间么 提交于 2019-12-04 02:23:00
I am relatively new to RXJava in general (really only started using it with RXJava2), and most documentation I can find tends to be RXJava1; I can usually translate between both now, but the entire Reactive stuff is so big, that it's an overwhelming API with good documentation (when you can find it). I'm trying to streamline my code, an I want to do it with baby steps. The first problem I want to solve is this common pattern I do a lot in my current project: You have a Request that, if successful, you will use to make a second request. If either fails, you need to be able to identify which one

How to continue processing after an error happens in RxJava 2?

霸气de小男生 提交于 2019-12-03 17:02:53
I have a PublishSubject and a Subscriber which I use to process a (possibly) infinite stream of preprocessed data. The problem is that some of the elements might contain some error. I'd like to ignore them and continue processing. How can I do so? I've tried something like this: val subject = PublishSubject.create<String>() subject.retry().subscribe({ println("next: $it") }, { println("error") }, { println("complete") }) subject.onNext("foo") subject.onNext("bar") subject.onError(RuntimeException()) subject.onNext("wom") subject.onComplete() My problem is that none of the error handling

Difference between Observable.create() and Observable.fromCallable()

帅比萌擦擦* 提交于 2019-12-03 12:39:06
Suppose we are getting a generic Object from SharedPrefs using .create() : return Observable.create(subscriber -> { String json = sharedPreferences.getString(key, ""); T myClass = gson.fromJson(json, generic); subscriber.onNext(myClass); subscriber.onComplete(); }); and using .fromCallable() : return Observable.fromCallable(() -> { String json = sharedPreferences.getString(key, ""); return gson.fromJson(json, generic); }); Is there any Difference if we call onComplete() immediately after first emmit from Observable.create() and using Observable.fromCallable() ? If so, what are the pros/cons ?

Unit testing Rxjava observables that have a delay

自闭症网瘾萝莉.ら 提交于 2019-12-03 12:22:09
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().test() myObservable.onNext(true) // val lock = CountDownLatch(1) // lock.await(3100, TimeUnit

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

a 夏天 提交于 2019-12-03 10:37:00
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. I can't understand what the difference between the two is. I've had a look at some articles but

Run void method in background

守給你的承諾、 提交于 2019-12-03 10:16:56
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 return type? The fromAction() method is what you're looking for. Completable.fromAction(this::myHeavyMethod)

How to convert rxJava2's Observable to Completable?

左心房为你撑大大i 提交于 2019-12-03 09:38:41
I have Observable stream, and I want to convert it to Completable, how I could do that? The fluent way is to use Observable.ignoreElements() . Observable.just(1, 2, 3) .ignoreElements() Convert it back via toObservable if needed. 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 process that Observable wraps. More details can be found here in the Java doc for the method . As I understand all

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

半城伤御伤魂 提交于 2019-12-03 07:03:48
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 be nice if the code above worked the same even without .share() . I would not have to think about when

Convert RxJava Observables To Live Data With Kotlin Extension Functions

五迷三道 提交于 2019-12-03 05:51:17
问题 I've been using alot of RxJava Observables converted to LiveData in my code using LiveDataReactiveStreams.fromPublisher() library. So I though of adding an extension function to the RxJava Observable to easily convert them to LiveData . These are my extension functions: fun <T> Flowable<T>.toLiveData() : LiveData<T> { return LiveDataReactiveStreams.fromPublisher(this) } fun <T> Observable<T>.toLiveData(backPressureStrategy: BackpressureStrategy) : LiveData<T> { return LiveDataReactiveStreams

Rxandroid What's the difference between SubscribeOn and ObserveOn

杀马特。学长 韩版系。学妹 提交于 2019-12-03 04:45:50
问题 I am just learning Rx-java and Rxandroid2 and I am just confused what is the major difference between in SubscribeOn and ObserveOn. 回答1: SubscribeOn specify the Scheduler on which an Observable will operate. ObserveOn specify the Scheduler on which an observer will observe this Observable. So basically SubscribeOn is mostly subscribed (executed) on a background thread ( you do not want to block the UI thread while waiting for the observable) and also in ObserveOn you want to observe the