rx-android

Retrofit “IllegalStateException: Already executed”

痴心易碎 提交于 2019-12-04 15:37:26
问题 I have a Retrofit network call that id like to run every 5 seconds. My current code: Handler h = new Handler(); int delay = 5000; //milliseconds h.postDelayed(new Runnable() { public void run() { call.enqueue(new Callback<ApiResponse>() { @Override public void onResponse(Response<ApiResponse> response) { Log.d("api", "response: " + response.body().getPosition().getLatitude().toString()); } @Override public void onFailure(Throwable t) { } }); h.postDelayed(this, delay); } }, delay); This runs

Custom rx Func1 for retrofit response code handling

谁说胖子不能爱 提交于 2019-12-04 15:12:08
I am new in rxjava, so please don't be strict... I have request lice next one: Observable<Login>login(String l, String p){ return api.loginUser(l,p) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .flatMap(new Func1<Response<Login>, Observable<? extends Login>>() { @Override public Observable<? extends Login> call(Response<Login> r) { switch (r.code()){ case 200: requestOk(r); break; case 202: //need to Repeat login.timeout(2000,TimeUnit.Milliseconds); break; default: // } } }); } Observable<Login> requestOk(final Response<Login> r){ return Observable.create(new

Observable, retry on error and cache only if completed

强颜欢笑 提交于 2019-12-04 08:06:47
问题 we can use the cache() operator to avoid executing a long task (http request) multiple times, and reuse its result: Observable apiCall = createApiCallObservable().cache(); // notice the .cache() --------------------------------------------- // the first time we need it apiCall.andSomeOtherStuff() .subscribe(subscriberA); --------------------------------------------- //in the future when we need it again apiCall.andSomeDifferentStuff() .subscribe(subscriberB); The first time, the http request

RxAndroid: UI changes on Schedulers.io() thread

折月煮酒 提交于 2019-12-04 07:36:56
I have simple job on IO thread which is changing home screen wallpaper, after that I'm trying to run some animation on UI thread: AppObservable.bindFragment(this, Observable.just(0)) .observeOn(Schedulers.io()) .subscribe(v -> setWallpaperOnSeparateThread()); private void setWallpaperOnSeparateThread() { WallpaperHelper.setBitmapAsWallpaper(photoViewAttacher.getVisibleRectangleBitmap(), getBaseActivity()); AppObservable.bindFragment(this, Observable.just(0)) .delay(500, TimeUnit.MILLISECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(integer -> loadFinishAnimationAfterSetWallpaper(

how to handle RxAndroid errors in the main thread

泄露秘密 提交于 2019-12-04 04:10:57
I'm new to rxJava/Android and surprised that my onError lambda is sometimes called on the main-thread and sometimes not, although I use .observeOn(AndroidSchedulers.mainThread()) Example 1 : onError on main-thread this works as expected: onError is called on the main-thread Observable.error(new RuntimeException("RTE")) .subscribeOn(Schedulers.newThread()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(s -> { Log.e(TAG, "onNext("+s+")-thread: " + Thread.currentThread().getName()); }, throwable -> { Log.e(TAG, "onError()-thread: " + Thread.currentThread().getName()); }); log-output:

RxAndroid button click Observer?

拟墨画扇 提交于 2019-12-03 21:37:33
Hi fellow programmers, I am using RxAndroid to make an interval API call every 3 seconds when a button is pressed. private final CompositeDisposable disposables = new CompositeDisposable(); Observable fetchWeatherInterval = Observable.interval(3, TimeUnit.SECONDS) .map(new Function<Long, String>() { @Override public String apply(Long aLong) throws Exception { return getWeather("http://samples.openweathermap.org/data/2.5/weather?", "London,uk", "b1b15e88fa797225412429c1c50c122a1"); } }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); Observer displayWeatherInterval =

When to unsubscribe a subscription

雨燕双飞 提交于 2019-12-03 19:09:27
问题 I have a question regarding how to unsubscribe an observable. I have two codes and I'm not really sure about which one is better. Example 1 -> Unsubscribe the subscriber once the stream has finished: Subscriber<String> subscriber = new Subscriber<String>() { @Override public void onCompleted() { progressdialog.dissmiss(); unsubscribe(); } @Override public void onError(Throwable e) { progressdialog.dissmiss(); } @Override public void onNext(String s) { // do something with data } } Example 2 -

Combine RxTextView Observable and Retrofit Observable

99封情书 提交于 2019-12-03 15:21:22
As an example to getting started with RxAndroid I'm trying to implement a searchbox which triggers a rest call when the users inserts something. So far I have two working parts. The first observing the EditTextView ... RxTextView.textChangeEvents(searchEditText) .debounce(400, TimeUnit.MILLISECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Observer<TextViewTextChangeEvent>() { @Override public void onCompleted() { Timber.d("onCompleted"); } @Override public void onError(Throwable e) { Timber.e(e, "onError"); } @Override public void onNext(TextViewTextChangeEvent e) { Timber.d(

How can multiple requests used to populate models be handled using RXJava Observables?

假装没事ソ 提交于 2019-12-03 13:50:39
We are using ReactiveX and Retrofit in our network stack to handle all API requests in an asynchronous way. Our goal is to create one method that will return a completely populated collection of User models. Each User model has a list of Pet objects. We can fetch all of the User models with one request. However, Pet models need to be requested per User . Getting users is simple: // Service.java @GET("users/?locationId={id}") Observable<List<User>> getUsersForLocation(@Path("id") int locationId); @GET("pets/?userId={id}") Observable<List<Pet>> getPetsForUser(@Path("id") int userId); //

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 ?