BVtp
BVtp

Reputation: 2480

Performing different types of Observables in sequence with RxJava/RxAndroid

I have a remote call(retrofit) - which I converted into an Observable. Let's call it Observable Y. Now, I also have a certain code that looks for geo location with GPS and NETWORK providers. I have a Timer there, that basically limits the time that the geo search can be performed for. Let's call it Task X. I want to convert it into an Observable X.

Then, I want to have a subcription, that will perform Observable X(that is, find location), once it will return a Location, I will "analyze" in a certain way, and then I will either pass that Location into the Observable Y(the retrofit call), or simply quit(if that "raw" location will be enough in my case)

At ALL time, I want to be able to interrupt all that "process". From what I gather, I can achieve that by simply unsubscribing the subscription, right? and then next time just subscribe this subscription once again in the future.

Questions:

1. Can all of that be implemented via RxJava/RxAndroid ?
2. Does it even make sense implementing it with Rx ? or is there a more efficient way?
3. How is it done with Rx? 
(More specifically : (a) How do I convert task Y into an Observable Y?
                     (b) How do I perform them in sequence with only one subscription?)

Upvotes: 1

Views: 253

Answers (1)

maheryhaja
maheryhaja

Reputation: 1687

1- It can be implemented via RxJava

2- This is your best option so far

3-

3-a Observable.fromCallable() does the trick

3-b flatmap operator is used to chain observable calls you can proceed like this:

private Location searchForLocation() {
    // of course you will return not null location
    return null;
}

// your task X
//mock your location fetching
private Observable<Location> getLocationObservableX() {
    return Observable.fromCallable(() -> searchForLocation());
}

//your task Y
//replace CustomData with simple String
//just to mock your asynchronous retrofit call
private Observable<List<String>> getRetrofitCallObservableY(String param){
    return Observable.just(new ArrayList<String>());
}


//subscribe
private void initialize() {
    getLocationObservableX()
            .filter(location -> {
                //place your if else here
                //condition
                //don't continue tu retrofit
                boolean condition = false;

                if (condition) {
                    //process
                    //quit and pass that Location in Broadcas
                    //you shall return false if you don't want to continue
                    return false;
                }
                return true;
            })
            //filter operation does not continue here if you return false
            .flatMap(location -> getRetrofitCallObservableY("param"))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(response -> {
                //do what you want with response
            });
}

Upvotes: 2

Related Questions