rx-java

RxJava concurrency with multiple subscribers and events

為{幸葍}努か 提交于 2020-01-05 05:35:24
问题 I'm looking for a way to attach multiple subscribers to an RxJava Observable stream, with each subscriber processing emitted events asynchronously. I first tried using .flatMap() but that didn't seem to work on any subsequent subscribers. All subscribers were processing events on the same thread. .flatMap(s -> Observable.just(s).subscribeOn(Schedulers.newThread())) What ended up working was consuming each event in a new thread by creating a new Observable each time: Observable.from(Arrays

State machine using RxJava?

独自空忆成欢 提交于 2020-01-05 04:39:08
问题 I'm trying to go all in on RxJava and solve this problem I have with it, but it seems very unsuited for it, as RxJava seems to not want to deal with having any sort of state, rather only passing along events and mutating them to handle them. The basic state machine behavior I'm trying to emulate with RxJava is this: On app start event, wait for the next app pause. On app pause event, start a 15 minute timer and wait for the next app resume. If the app resumes within the timer, cancel it and

onNext() onError() callbacks not executing on second subscribtion android

旧巷老猫 提交于 2020-01-05 03:55:07
问题 I've an Observable something like this: @GET("endpoint") Observable<Something> getSomething(); and Subscriber like this Subscriber<Something> somethingSubscriber = new Subscriber<Something>() { public void onCompleted() { } public void onError(Throwable e) { //handle exceptions } public void onNext() { //do something } In my OnClickListener associated with a button, i make a subscription getSomething() .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe

How to make Observable that emit character after 1 second of time interval

旧巷老猫 提交于 2020-01-04 18:31:20
问题 I have just started with RxJava/android and for practice and getting started I want to make observable that emits character in string every 1 second, how can I do this? Here is what I have tried so far, its just emit string at once: Observable<String> myObservable = Observable.interval(5000L, TimeUnit.MILLISECONDS) .observeOn(AndroidSchedulers.mainThread()) .just("Hello"); I want hello like this first emit H after 1 second emit E and so on 回答1: Split String "hello" into letters "h", "e", ...

RxJava android mvp unit test NullPointerException

可紊 提交于 2020-01-04 13:06:24
问题 I am new to Unit Testing in mvp, I want to make a verrry basic test to the presenter, which is responsible for making login, I just want to assert on view.onLoginSuccess(); Here is the PresenterCode: public LoginPresenter(LoginViewContract loginView, LoginModelContract loginModel, CurrentUserLoginModelContract currentUserLoginModel, CompositeDisposable subscriptions) { this.subscriptions = subscriptions; this.loginView = loginView; this.loginModel = loginModel; this.currentUserLoginModel =

RxJava - “Only one emission is allowed to travel up the Observable chain at a time…”

我们两清 提交于 2020-01-04 09:48:17
问题 I'm reading a blog post here: http://tomstechnicalblog.blogspot.com/2016/02/rxjava-understanding-observeon-and.html where it is said that No matter what Scheduler you are subscribed on, only one emission is allowed to travel up the Observable chain of operators at a time. Below, you can observe that the emission must be pushed all the way from the source to the Subscriber before the next emission can start. Right above that quoted text is an example written as: public static void main(String[

Using Retrofit and RxJava, how do I deserialize JSON when it doesn't map directly to a model object?

怎甘沉沦 提交于 2020-01-04 05:41:16
问题 The API I'm working with returns objects (and their containing objects) in a "flat" format and I'm having trouble getting this to work elegantly with Retrofit and RxJava. Consider this JSON response for an /employees/{id} endpoint: { "id": "123", "id_to_name": { "123" : "John Doe" }, "id_to_age": { "123" : 30 } } Using Retrofit and RxJava, how do I deserialize this to a Employee object with fields for name and age ? Ideally I'd like RxJava's onNext method to be called with an Employee object.

Repeatedly make API call with retrofit and rxjava

馋奶兔 提交于 2020-01-04 03:59:34
问题 I have a retrofit observable: @GET("something/") Observable<Something> getSomething(); subscribing to it gives the response. getSomething().subscribe(new Subscriber<Something>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(Something something) { //update database of something } }); how can i make this call every 60 seconds so that i can update database accordingly? 回答1: First please don't do it if you can avoid it. It's

rxJava, Refresh api data periodically

允我心安 提交于 2020-01-04 02:30:46
问题 I am using the following observable to call retrofit api then save the response into cache file: @Override public Observable<StoryCollectionEntity> storyEntityList(final int page) { return this.restApi.storyCollection(id, page) .doOnNext(saveStoryCollectionToCacheAction) .onErrorResumeNext(CloudNewsDataStore.this.mNewsCache.getStories(page)); } This works as expected. my question is: how can i make this observer returns api response periodically? let's say, user wants to refresh the data

Debounce only false values in boolean observable

冷暖自知 提交于 2020-01-03 21:50:27
问题 I have a boolean observable that (through a observer) starts and stops an Android service. When true is passed, the service must start immediately. When false is passed, I'd like to debounce it in case a true comes along soon after, to avoid needless (and disruptive) stopping and starting of the service. Is there a composition of standard operators that can do this, or must I write my own, and if so, should I base it on OperatorDebounceWithTime.java or is there a simpler way? Thanks! 回答1: It