rx-java

Apply retries in a RXjava

假装没事ソ 提交于 2019-12-19 08:20:13
问题 I want to run a method with retries using RXJava return Observable .just(myObj) .flatMap(doc -> myFunc(myObj, ....) ) .doOnError(e -> log.Error()) .onErrorResumeNext(myObj2 -> methodIWantToRunWithRetries(...) .onErrorResumeNext(myObj3 -> methodIWantToRunWithRetries(...) ) ); } If I use onErrorResumeNext I need to nest it as many times I want my retries. (unless I want to surround it with try/catch) Is there an option to implement it with RXJava methods? 回答1: RxJava offers standard retry

How to debounce a retrofit reactive request in java?

拟墨画扇 提交于 2019-12-19 05:23:19
问题 I'm working on an android project that makes requests through retrofit using Rx-Java observable and subscribe. However, in some interactions this request can be called multiple times and I would like to only execute the last one in a predefined window of time (debounce). I tried to apply the debounce operator directly to the observable, but it will not work because the code below is executed every time some interaction occurs: mApi.getOnlineUsers() .debounce(1, TimeUnit.SECONDS) .subscribe(..

Reactive approach to simple imperative task

跟風遠走 提交于 2019-12-19 02:57:25
问题 Application requirements: subscribe to two event streams A and B for each A event there should be corresponding B event some time later the application monitors A events and alerts if no corresponding B arrives (in time) B events can arrive in a different order from the A events (but only after), late, or not at all This is simple in a traditional approach: record each A event in a collection remove the A event when a corresponding B event arrives monitor the collection for A events that don

How to handle auth0 403 error without adding specific code everywhere (Retrofit/okhttp/RxAndroid)

倖福魔咒の 提交于 2019-12-18 15:32:14
问题 I am using Auth0, which gives me a JWT (json web token) and a refreshtoken. I use this JWT in the http headers to communicate with my backend. It could happen, that the server gives me a 403 , when it decides that the JWT has expired. In this event, I can ask Auth0 to issue me a new JWT, using the refreshtoken. It means I call the Auth0 backend, pass it the refreshtoken, and it gives me a new JWT, which I can then use in my requests. My question is, how can I efficiently write this behaviour

Handle empty response with retrofit and rxjava 2.x

落花浮王杯 提交于 2019-12-18 14:27:03
问题 When using rxjava 1.x i used to return Observable<Void> to handle empty response from retrofit: @POST( "login" ) Observable<Void> getToken( @Header( "Authorization" ) String authorization, @Header( "username" ) String username, @Header( "password" ) String password ); But since rxjava 2.x won't emit anything with Void is there any good practice to handle those empty responses? 回答1: Completable was designed for such cases. It available since RxJava 1.1.1. From the official docs: Represents a

Handle empty response with retrofit and rxjava 2.x

无人久伴 提交于 2019-12-18 14:24:01
问题 When using rxjava 1.x i used to return Observable<Void> to handle empty response from retrofit: @POST( "login" ) Observable<Void> getToken( @Header( "Authorization" ) String authorization, @Header( "username" ) String username, @Header( "password" ) String password ); But since rxjava 2.x won't emit anything with Void is there any good practice to handle those empty responses? 回答1: Completable was designed for such cases. It available since RxJava 1.1.1. From the official docs: Represents a

Retrofit 2 + Rxjava handling error

ⅰ亾dé卋堺 提交于 2019-12-18 13:16:32
问题 So i already receive a token from the Json when the login is made without problems, and receive the hash But when the response from the server is a error, i cannot get the Json message ({message:"Error : wrong email} " because in the onError we only get as argument a Throwable and not a model class like in on how can i get a json message from server onError?? final Observable<TokenResponse> observable = Service.login(userCredentials); observable.subscribeOn(Schedulers.io()) .observeOn

RxJava: How to conditionally apply Operators to an Observable without breaking the chain

你。 提交于 2019-12-18 13:02:00
问题 I have a chain of operators on an RxJava observable. I'd like to be able to apply one of two operators depending on a boolean value without "breaking the chain". I'm relatively new to Rx(Java) and I feel like there's probably a more idiomatic and readable way of doing this than my current approach of introducing a temporary variable. Here's a concrete example, buffering items from an observable if a batch size field is non-null, otherwise emitting a single batch of unbounded size with toList(

RxJava: difference between doOnNext and doOnEach

心已入冬 提交于 2019-12-18 12:04:57
问题 In which cases I should use doOnNext, and in which cases doOnEach? .doOnEach(new Action1<Notification<? super MessageProfile>>() { @Override public void call(Notification<? super MessageProfile> notification) { } }) .doOnNext(new Action1<MessageProfile>() { @Override public void call(MessageProfile profile) { messageProfileDao.save(profile); } }) This looks like the both operators have the same effect. 回答1: They are indeed quite close. One thing that differs (and it's maybe not that clear in

How to get notified of a observer's unsubscribe action in a custom Observable in RxJava

*爱你&永不变心* 提交于 2019-12-18 11:46:39
问题 I'm trying to wrap some listener-pattern based API to an Observable. My code roughly looks like following. def myObservable = Observable.create({ aSubscriber -> val listener = {event -> aSubscriber.onNext(event); } existingEventSource.addListener(listener) }) However, I want my observable to immediately remove the listener from the underlying existingEventSource when the observer calls subscription.unscribe(). How could I achieve this goal? 回答1: The Subscriber abstract class actually has a