Skip to content

Instantly share code, notes, and snippets.

Avatar
🇦🇷
https://feresr.github.io/

Fernando Raviola feresr

🇦🇷
https://feresr.github.io/
View GitHub Profile
View RxJavaSimpleExample.class
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getData(new DataSubscriber());
}
});
public void getData(DataSubscriber subscriber) {
Subscription s = apiEndpoints.getData()
View step2.java
Observable<Data> dataStream = RxView.clicks(button)
.flatMap(new Func1<Void, Observable<Data>>() {
@Override
public Observable<Data> call(Void aVoid) {
return apiEndpoints.getData();
}
});
View sample4.java
RxView.clicks(button)
.observeOn(Schedulers.io())
.debounce(2, TimeUnit.SECONDS)
.flatMap(aVoid -> apiEndpoints.getData())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
@feresr
feresr / errorHandling.java
Created Jan 30, 2017
Error handling response
View errorHandling.java
@Override public void onNext(DataResponse response) {
if (response.isSuccessful()) {
displayData(response.getData());
} else {
showError(response.getError());
}
}
View errorHandling.java
@Override public void onNext(DataResponse response) {
if (response.isSuccessful()) {
displayData(response.getData());
} else {
showError(response.getError());
}
}
View errorHandling.java
observableThatShouldNotCompleteOnError
.flatMap(e -> obsrevableThatMayThrow(e)
.onErrorResumeNext(Observable.empty())
).subscribe()
View multicast.java
Observable<Data> dataStream = RxView.clicks(button)... //create the stream as before
dataStream.subscribe(data -> Log.d(TAG, "View one received the data"));
dataStream.subscribe(data -> Log.d(TAG, "View two received the data"));
View multicast2.java
Observable<Data> dataStream = Observable.just(1)
.observeOn(Schedulers.io())
.debounce(2, TimeUnit.SECONDS)
.flatMap(new Func1<Integer, Observable<Data>>() {
@Override
public Observable<Data> call(Integer aVoid) {
Log.d(TAG, "Performing network request...");
return apiEndpoints.getData();
}
})
View multicasting3.java
Observable<Data> dataStream = RxView.clicks(button)
.observeOn(Schedulers.io())
.debounce(2, TimeUnit.SECONDS)
.flatMap(new Func1<Void, Observable<Data>>() {
@Override
public Observable<Data> call(Void aVoid) {
Log.d(TAG, "Performing network request...");
return apiEndpoints.getData();
}
})
View rxExample1.java
button.setOnClickListener(v -> getData(new DataSubscriber()));
public void getData(DataSubscriber subscriber) {
Subscription s = apiEndpoints.getData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
compositeSubscriptions.add(s);
}