compile 'io.reactivex.rxjava2:rxjava:2.0.0'Single.just(1).subscribe(new SingleObserver<Integer>() {
@Override
public void onSubscribe(Disposable d) {
// Subscription just happened
}
@Override
public void onSuccess(Integer value) {
// Succeeded with a value
}
@Override
public void onError(Throwable e) {
// Failed
}
});DogCache.getDog("Morzsi").subscribe(new MaybeObserver<Dog>() {
@Override
public void onSubscribe(Disposable d) {
// Subscription just happened
}
@Override
public void onSuccess(Dog value) {
// We got Morzsi \O/
}
@Override
public void onError(Throwable e) {
// Error during getting Mittens
}
@Override
public void onComplete() {
// Where is Morzsi? :(
}
});Completable.fromRunnable(expenseOperation).subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
// Subscription just happened
}
@Override
public void onComplete() {
// Operation complete
}
@Override
public void onError(Throwable e) {
// Operation failed
}
});Observable.just(1, 2, 3, 4).subscribe(new Observer<Integer>() {
@Override
public void onSubscribe(Disposable d) {
// Subscription just happened, we can dispose right away
}
@Override
public void onNext(Integer value) {
// Got a new event
}
@Override
public void onError(Throwable e) {
// Ended with error
}
@Override
public void onComplete() {
// Ended with completion
}
});CompositeDisposable cd = new CompositeDisposable();
Disposable d1 = Observable.just(1).subscribe(System.out::println);
Disposable d2 = Observable.just(2).subscribe(System.out::println);
cd.add(d1);
cd.add(d2);
cd.clear();Flowable.range(1, 2000).subscribe(new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
// Subscription just happened
s.request(5);
}
@Override
public void onNext(Integer integer) {
// Got a new event
}
@Override
public void onError(Throwable t) {
// Ended with error
}
@Override
public void onComplete() {
// Ended with completion
}
});CompositeDisposable cd = new CompositeDisposable();
ResourceSubscriber s1 = Flowable.range(1, 2000).subscribeWith(resourceSubscriber);
ResourceSubscriber s2 = Flowable.range(1, 2000).subscribeWith(resourceSubscriber);
cd.add(s1);
cd.add(s2);
cd.clear();Observable.just(1, 2, 3)
.test()
.assertSubscribed()
.assertResult(1, 2, 3);Writing operators, source-like (fromAsync) or intermediate-like (flatMap) has always been a hard task to do in RxJava. There are many rules to obey, many cases to consider but at the same time, many (legal) shortcuts to take to build a well performing code. Now writing an operator specifically for 2.x is 10 times harder than for 1.x. If you want to exploit all the advanced, 4th generation features, that's even 2-3 times harder on top (so 30 times harder in total).