rx-java

Android Pros & Cons: Event Bus and RxJava

不打扰是莪最后的温柔 提交于 2019-12-18 11:40:09
问题 I have been using Event Bus in my apps (i.e: greenrobot/EventBus). But I find some disadvantages in using Event Bus: Chaining tasks execution is difficult A lot of classes to represent events Less clear code (well, it's still possible to trace, but not as clear) I have been researching for new techniques to deal with this problem. And I read quite a bit about RxJava and wonder if it could be a solution. So my questions about RxJava (based on what I have read recently): Could RxJava observer

RxJava Observing on calling/subscribing thread

不想你离开。 提交于 2019-12-18 10:24:05
问题 I have some trouble understandig how subscribeOn/observeOn works in RxJava. I've created simple app with observable that emits solar system planet names, does some mapping and filtering and prints results. As I understand, scheduling work to background thread is done via subscribeOn operator (and it seems to work fine). Observing on background thread also works fine with observeOn operator. But I have trouble in understanding, how to observe on calling thread (either if it is main thread or

Retrofit/Rxjava and session-based services

心不动则不痛 提交于 2019-12-18 10:19:19
问题 I'm implementing session-based services. All requests have to be subscribed with a cookie session param, which in turn is retrieved with separate rest api. So the basic workflow would be to get the session cookie and proceed querying the services. Occasionally the cookie would expire and it would lead to another session cookie request. I'm trying to make client code session-agnostic, so that it doesn't have to worry about maintaining session, but rather I want it to be hidden inside services

In RxJava, how to pass a variable along when chaining observables?

匆匆过客 提交于 2019-12-18 10:18:06
问题 I am chaining async operations using RxJava, and I'd like to pass some variable downstream: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1); }) ... .flatmap( (data2) -> { // How to access data1 here ? return op2(data2); }) It seems like a common pattern but I couldn't find information about it. 回答1: The advice I got from the Couchbase forum is to use nested observables: Observable .from(modifications) .flatmap( (data1) -> { return op1(data1) ... .flatmap( (data2) -> {

Retrofit with Rxjava Schedulers.newThread() vs Schedulers.io()

假装没事ソ 提交于 2019-12-18 10:15:02
问题 What are the benefits to use Schedulers.newThread() vs Schedulers.io() in Retrofit network request. I have seen many examples that use io() , but I want to understand why. Example situation: observable.onErrorResumeNext(refreshTokenAndRetry(observable)) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread())... vs observable.onErrorResumeNext(refreshTokenAndRetry(observable)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread())... One of the reasons

AsyncTask replacement with RXJava help needed

ぃ、小莉子 提交于 2019-12-18 05:17:11
问题 I am new to Reactive so go easy on me, but I am trying to replace an async task that is currently run on text changes for an auto suggest function. Below is where I am at with my RX: rest.getMemberList(etSearch.getText().toString())//rest interface .subscribeOn(Schedulers.io()) .observeOn(RxAndroidSchedulers.mainThread()) .subscribe(new Action1<List<Member>>() { @Override public void call(List<Member> results) { ListView ResultsListView = (ListView) findViewById(R.id.list); MemberAdapter =

How do I implement polling using Observables?

血红的双手。 提交于 2019-12-18 03:35:11
问题 I have a parametrized rest call that should be executed every five seconds with different params: Observable<TResult> restCall = api.method1(param1); I need to create an Observable<TResult> which will poll the restCall every 5 seconds with different values for param1. If the api call fails I need to get an error and make the next call in 5 seconds. The interval between calls should be measured only when restCall is finished (success/error). I'm currently using RxJava, but a .NET example would

Using RxJava and Okhttp

孤街醉人 提交于 2019-12-17 23:27:15
问题 I want to request to a url using okhttp in another thread (like IO thread) and get Response in the Android main thread, But I don't know how to create an Observable . 回答1: First add RxAndroid to your dependencies, then create your Observable like this: Subscription subscription = Observable.create(new Observable.OnSubscribe<Response>() { OkHttpClient client = new OkHttpClient(); @Override public void call(Subscriber<? super Response> subscriber) { try { Response response = client.newCall(new

My subscriber's onNext and onComplete functions do not run when I call onNext within my FlowableOnSubscribe class

倖福魔咒の 提交于 2019-12-17 21:04:31
问题 In an Android project that uses RxJava 2, I create a Flowable like this in the onCreate of my initial activity: Flowable.create(new MyFlowableOnSubscribe1(), BackpressureStrategy.BUFFER) .doOnComplete(new MyAction()) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new MySubscriber()); The implementation of the FlowableOnSubscribe is: public class MyFlowableOnSubscribe1 implements FlowableOnSubscribe<String> { public static final String TAG = "XX MyFlOnSub1"

CustomObservable vs Observable.create()?

感情迁移 提交于 2019-12-17 20:02:17
问题 I'm working on wrapping listeners into Observables. Normally we use Observable.create() to wrap that but there are libraries prefer to use custom Observables and wrap things inside of it such as RxBinding , or even RxJava itself (from what my understanding, operators are basically create new Custom Observable to do operation inside of it). So my questions are: what benefits we can get from Custom Observable? It's just lower overhead than Observable.create() since we don't generate another