I'm working on AndroidTv where I'm using the standard seekBar for playback.
This is how I'm using the RxSeekBar
RxSeekBar.changeEvents(seekBar)
.debounce(SEEKBAR_DEBOUNCE_TIME, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<SeekBarChangeEvent>() {
#Override
public void onSubscribe(Disposable disposable) {
compositeDisposable.add(disposable);
}
#Override
public void onNext(SeekBarChangeEvent seekBarChangeEvent) {
if (seekBarChangeEvent instanceof SeekBarProgressChangeEvent) {
if (seekBar.hasFocus()) {
DebugLog.d(TAG, "(PROGRESS) SeekBarProgressChangeEvent");
seekBarSeekProgress = seekBarChangeEvent.getView().getProgress();
onSeek();
}
}
}
#Override
public void onError(Throwable e) {
DebugLog.d(TAG, "RxSeek error - " + e.getMessage());
}
#Override
public void onComplete() {
}
});
Basically, I want to update things after the debounce timeout. This works fine 7/10 times but sometimes onNext is called prematurely and things go off hand.
Am I doing something wrong here?
Related
I use the following code to realize the countdown display at the millisecond level. After onSubscribe is executed, the onNext method is executed nearly one minute later.
This problem occurs occasionally. This problem also exists when creating Observable using the create method.
I don't have any solutions at present. Please help me, thank you
Observable.interval(1900, 50, TimeUnit.MILLISECONDS).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnDispose(() -> {
XLog.i("doOnDispose");
})
.compose(RxUtils.bindToLifecycle(provider))
.subscribe(new Observer<Long>() {
#Override
public void onSubscribe(#NonNull Disposable d) {
XLog.i("onSubscribe");
}
#Override
public void onNext(#NonNull Long aLong) {
XLog.i("onNext");
// refresh ui countdown
}
#Override
public void onError(#NonNull Throwable e) {
XLog.i("onError");
}
#Override
public void onComplete() {
XLog.i("onComplete");
}
});
I don't have any solutions at present
I trying to filter the list after taking the input from edittext ,Actually its filtering the list but i want to group the result to list.
So I used to list operator with SingleObserver but result is not coming; why is this?
RxTextView.textChangeEvents(searchEdit)
.skip(1)
.debounce(400, TimeUnit.MILLISECONDS)
.switchMap(new Function<TextViewTextChangeEvent, Observable<List<VehicleMakeModel>>>() {
#Override
public Observable<List<VehicleMakeModel>> apply(TextViewTextChangeEvent textViewTextChangeEvent) throws Exception {
return Observable.just(variantlist);
}
})
.flatMapIterable(new Function<List<VehicleMakeModel>, List<VehicleMakeModel>>() {
#Override
public List<VehicleMakeModel> apply(List<VehicleMakeModel> v) {
return v;
}
})
.filter(new Predicate<VehicleMakeModel>() {
#Override
public boolean test(VehicleMakeModel v) {
if (searchEdit.getText().toString().isEmpty())
return true;
else
return v.getVariant().toLowerCase().trim().contains(searchEdit.getText().toString().toLowerCase().trim());
}
})
.map(new Function<VehicleMakeModel, VehicleMakeModel>() {
#Override
public VehicleMakeModel apply(VehicleMakeModel integer) throws Exception {
return integer;
}
})
.toList()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<VehicleMakeModel>>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onSuccess(List<VehicleMakeModel> vehicleMakeModels) {
System.out.println("testing");
}
#Override
public void onError(Throwable e) {
}
});
There is a must-remember fact about RxBinding: event observables never complete.
RxTextView.textChangeEvents(searchEdit) will never call the complete callback, so toList() won't never return your expected list.
This is not a bug, but a wanted behavior for RxBinding because your textChanges observable will never stop listening for that event.
I am new to reactive programming (RxJava and RxAndroid). I want to use RxView.clicks() instead of a click Listener. I put a Button into main layer and with Butterknife and in onCreate method Main activity I write this statement:
**//onCreate**
ButterKnife.bind(this);
RxView.clicks(btn_range)
.switchMap(new Function<Object, Observable<Integer>>() {
#Override
public Observable<Integer> apply(Object o) throws Exception {
return Observable.range(1,10);
}
})
.subscribe(new Observer<Integer>() {
#Override
public void onSubscribe(Disposable d) {
d.dispose();
}
#Override
public void onNext(Integer integer) {
Toast.makeText(MainActivity.this, integer+"", Toast.LENGTH_SHORT).show();
}
#Override
public void onError(Throwable e) {
Toast.makeText(MainActivity.this, e.getMessage()+"", Toast.LENGTH_SHORT).show();
}
#Override
public void onComplete() {
}
});
but when I run it, no toast appears.
I have converted my click listener to an observable and then I have changed the observable to a range of integer and finally I display it.
In your .subscribe() the Observer<Integer> calls d.dispose() as soon as it is subscribed.
So if your chain is disposed then it is not working anymore. The Disposable should be disposed when you no longer need the flow.
You could store the emitted Disposable and dispose it in the opposite lifecycle event callback to where you have subscribed it.
I am using RxJava2 and Retrofit2 for handling network requests.
I have cycle where doOnNext should always be ran, but my Activity which is the observer calls dispose() when it is destroyed and that causes retrofit to cancel the request.
java.io.IOException: Canceled
Is there a way to let the request complete but only dispose the UI level observer?
mApi.doSomethingImportant()
.doOnNext(new Consumer<ImportantResponse>() {
#Override
public void accept(ImportantResponse response) throws Exception {
// Store data, should always get here if request is success
}
})
.doOnError(new Consumer<Throwable>() {
#Override
public void accept(Throwable throwable) throws Exception {
// Store error, should always get here if request fails
}
})
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(AndroidSchedulers.mainThread())
.subscribe(observer); // observer reports success/fail on UI if not disposed
Thanks.
I found one way to achieve the wanted behaviour by wrapping the observer with another, but I'm sure there is some elegant way to do this.
...
.subscribe(new NonDisposableObserver<>(observer)
Where NonDisposableObserver class is following:
public class NonDisposableObserver<T> implements Observer<T> {
private DisposableObserver<T> observer;
public NonDisposableObserver(DisposableObserver<T> observer) {
this.observer = observer;
}
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(T t) {
if (!observer.isDisposed()) {
observer.onNext(t);
}
}
#Override
public void onError(Throwable e) {
if (!observer.isDisposed()) {
observer.onError(e);
}
}
#Override
public void onComplete() {
if (!observer.isDisposed()) {
observer.onComplete();
}
}
}
I have started learning RxAndroid and below is the code I wrote to iterate over a model object (Results) that contains data fetched from the server. I'm iterating over the model object in the observable and providing a newly created object in the observer. I'm trying to take subscription of the observer to unsubscribe the task upon Orientation changes of the fragment. However the subscribe() returns VOID instead of subscription object.
Questions:
Does the latest version of RxAndroid handle unsubscription itself upon configuration/orientation change?
In case configuration change happens before the task is complete, the only way to restart this task that I can think of is, I persist the server response in onSavedInstance() and retrieve it from bundle when the fragment is recreated. It'll require booleans to figure out if the configuration change happened before the configuration change or not. Is there a graceful and cleaner way of coping with this?
private void createComicList(final List<Result> marvelResults) {
final MarvelComics marvelComics = new MarvelComics();
Observable marvelObservable2 = Observable.create(new ObservableOnSubscribe<MarvelComic>() {
#Override
public void subscribe(ObservableEmitter<MarvelComic> e) throws Exception {
for(Result result : marvelResults) {
MarvelComic marvelComic = new MarvelComic();
marvelComic.setDescription(result.getDescription());
marvelComic.setTitle(result.getTitle());
marvelComic.setPageCount(result.getPageCount());
marvelComic.setThumbnailUrl(result.getThumbnail().getPath());
marvelComic.setId(result.getId());
e.onNext(marvelComic);
}
e.onComplete();
}
});
marvelObservable2.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<MarvelComic>() {
#Override
public void onSubscribe(Disposable d) {
}
#Override
public void onNext(MarvelComic comic) {
marvelComics.getMarvelComicList().add(comic);
}
#Override
public void onError(Throwable e) {
}
#Override
public void onComplete() {
showToast();
}
});
}
The Observable.subscribe(Observer<? super T>) method returns void in the 2.x since the Observer.onSubscribe(Disposable) is there to get the cancellation support that used to be Subscription in 1.x.
final CompositeDisposable composite = new CompositeDisposable();
Observable<Integer> source = Observable.just(1)
source.subscribe(new Observer<Integer>() {
#Override public void onSubscribe(Disposable d) {
composite.add(d); // <---------------------------------------------
}
#Override public void onNext(Integer t) {
System.out.println(t);
}
#Override public void onError(Throwable e) {
e.printStackTrace();
}
#Override public void onComplete() {
System.out.println("Done");
}
});
composite.add(source
.subscribeWith( // <-----------------------------------------------
new DisposableObserver<Integer>() {
#Override public void onNext(Integer t) {
System.out.println(t);
}
#Override public void onError(Throwable e) {
e.printStackTrace();
}
#Override public void onComplete() {
System.out.println("Done");
}
});
subscribe() method of Observable returns Subscription object in earlier versions of RxJava and current version returns an object of Disposble class which you can unsubscribe by invoking dispose() method.
For your second question you may check this answer Best practice: AsyncTask during orientation change