I am working on android app registration in which I need to perform several tasks one by one let say
task1
task2
task3
I want to chain these tasks one after another and if a task is failed whole process should be failed..
I want to solve this problem by Rxjava can anyone tell me how to achieve this with rxJava.
(I have wasted 5 hours but did not find solution also newbie in RxJava)
What I have tried
Observable.merge(task1,task2,task3).subscribe(new Observer<DataError>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(DataError dataError) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
//this method is not called by rxJava
}
});
with this method all things are working fine but onComplete() method is not called by rxJava .
Please help~
Edit-
Each task is dependent on previous task result.and there should be one task at one time.
Let's say we have 3 tasks. From given array of Integers, Find even numbers, Multiply each even numbers with 10, Divide each number by 2
//A stream of observable to find even numbers
private Observable<Integer> findEven(Integer number) {
return Observable
.just(number)
.filter(data -> data % 2 == 0);
}
//A stream of observable to multiply each number with 10
private Observable<Integer> multiplyBy10(Integer evenNumber) {
return Observable.just(evenNumber).map(data -> data * 10);
}
//A stream of observable to divide each number with 2
private Observable<Integer> divideBy2(Integer evenNumber) {
return Observable.just(evenNumber).map(data -> data / 2);
}
So, how do we chain this task?
Observable
.just(1, 2, 3, 4, 5, 6, 7, 8)
//find even numbers
.flatMap(num -> findEven(num))
//Now multiply each even number by 10
.flatMap(num -> multiplyBy10(num))
//Now to each number divide 2
.flatMap(num -> divideBy2(num))
.subscribe(
// result: 10, 20, 30, 40
result -> Log.v("", "result: " + result),
error -> Log.e("", error.getMessage())
);
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With