0

Basically I need to create a List of Observables without initial values. I subscribe to that list of Observables and will provide required results based on the responses from all Observables. I use zip operator.

The problem is that I need to create Observables initially, add them to the list and use zip operator. Only later I do network request with Retrofit and I need to update the value of observable in the list so my whole zip operator will be working.

However, I did not find a way to force update an observable in the list with the response from Retrofit. It seems very easy but I did not found any solutions.. only idea is to use tons of subjects and add them to the list instead...

 List<Observable<Object>> listObservables = new ArrayList<>();
    //Adding initial request
    Observable<Object> testObservable = RetrofitFactory.create().startProcess();
    listObservables.add(testObservable);

    Observable.concatDelayError(listObservables).subscribe(response ->
            {
                //This is where all results should be managed
                Log.d("response", String.valueOf(response));
            },
            error ->
            {
                Log.d("response", String.valueOf(error));
            });
    //Actual request occurs much later in application

    listObservables.get(0).subscribeOn(Schedulers.io()).
            observeOn(AndroidSchedulers.mainThread()).subscribe(response ->
    {
        // the response of this, should notify concatDelayError
        Log.d("respoonse", String.valueOf(response));
    });
halfer
  • 19,824
  • 17
  • 99
  • 186
Viktor Vostrikov
  • 1,322
  • 3
  • 19
  • 36

1 Answers1

0

If I understand correctly, you want to implement sub requests model. For this task you can break chain of operators to different execution flows and combine it back, for example, with zip operator. With this approach you can create completely independent data flow with sole trigger.

Subject<Event> eventSubject = PublishSubject.create();

Observable<TriggerObject> mainRequest = eventSubject.flatMap((event) -> 
        RetrofitFactory.create().startProcess());
Observable<FirstSubResult> firstSubRequest = mainRequest.flatMap(tigger -> {
    // make first sub request
});
Observable<SecondSubResult> secondSubRequest = mainRequest.flatMap(tigger -> {
    // make second sub request
});
Observable<CompleteResult> resultObservable = Observable.zip(firstSubRequest, secondSubRequest,
        // zip function
        (first, second) -> {
            // combine result of sub requests to complete result object  
        });

Now you can start request flow by your event:

// post your event. On button clicked for evxample
eventSubject.doOnNext(yourEvent);

NOTE: this answer show main idea of chaining data flow sequences. This applicable to to other types of requests, and you can use this approach without retrofit

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
  • Wow really genious slution.. It might be a case that need just this case – Viktor Vostrikov Feb 28 '19 at 13:10
  • Still not what I need exactly... Observable secondSubRequest = mainRequest.flatMap(tigger -> { // make second sub request }); this should have additional trigger, because it does not only needs to wait for startProcess() to complete but actually for user to press button ... – Viktor Vostrikov Feb 28 '19 at 14:05
  • @ViktorVostrikov for this purpose you can use `Subject` or [convenient libraries](https://stackoverflow.com/a/33652347/3926506). I've updated my answer – Sergei Bubenshchikov Mar 01 '19 at 08:03