Create an Observable that generates a Single on subscribe, but which stops and reuses a specific value





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm using Retrofit to make an API call, which returns a Single, and I use onErrorReturn to convert any exceptions to a default object. I want consumers to see the current value, but if the current value is the default object, I want to try to requery the API and send that result out in addition. To complicate things, I might have multiple subscribers to this.



So, I know the Retrofit Single has to be converted to a proper Observable stream and not just an onNext/onComplete like a normal Single.toObservable would, but I don't know how to requery the API and push the value back out to my previous subscribers using only the Single from Retrofit.



Right now, I do:



fun request(): Observable<Foo> {
if (behaviorSubject.value == defaultObject) {
API
.request()
.onErrorReturn(defaultObject)
.subscribe(behaviorSubject)
}
return behaviorSubject
}


But I know that calling subscribe is violating Rx chaining, so I'm trying to figure out how to get rid of that.










share|improve this question





























    0















    I'm using Retrofit to make an API call, which returns a Single, and I use onErrorReturn to convert any exceptions to a default object. I want consumers to see the current value, but if the current value is the default object, I want to try to requery the API and send that result out in addition. To complicate things, I might have multiple subscribers to this.



    So, I know the Retrofit Single has to be converted to a proper Observable stream and not just an onNext/onComplete like a normal Single.toObservable would, but I don't know how to requery the API and push the value back out to my previous subscribers using only the Single from Retrofit.



    Right now, I do:



    fun request(): Observable<Foo> {
    if (behaviorSubject.value == defaultObject) {
    API
    .request()
    .onErrorReturn(defaultObject)
    .subscribe(behaviorSubject)
    }
    return behaviorSubject
    }


    But I know that calling subscribe is violating Rx chaining, so I'm trying to figure out how to get rid of that.










    share|improve this question

























      0












      0








      0








      I'm using Retrofit to make an API call, which returns a Single, and I use onErrorReturn to convert any exceptions to a default object. I want consumers to see the current value, but if the current value is the default object, I want to try to requery the API and send that result out in addition. To complicate things, I might have multiple subscribers to this.



      So, I know the Retrofit Single has to be converted to a proper Observable stream and not just an onNext/onComplete like a normal Single.toObservable would, but I don't know how to requery the API and push the value back out to my previous subscribers using only the Single from Retrofit.



      Right now, I do:



      fun request(): Observable<Foo> {
      if (behaviorSubject.value == defaultObject) {
      API
      .request()
      .onErrorReturn(defaultObject)
      .subscribe(behaviorSubject)
      }
      return behaviorSubject
      }


      But I know that calling subscribe is violating Rx chaining, so I'm trying to figure out how to get rid of that.










      share|improve this question














      I'm using Retrofit to make an API call, which returns a Single, and I use onErrorReturn to convert any exceptions to a default object. I want consumers to see the current value, but if the current value is the default object, I want to try to requery the API and send that result out in addition. To complicate things, I might have multiple subscribers to this.



      So, I know the Retrofit Single has to be converted to a proper Observable stream and not just an onNext/onComplete like a normal Single.toObservable would, but I don't know how to requery the API and push the value back out to my previous subscribers using only the Single from Retrofit.



      Right now, I do:



      fun request(): Observable<Foo> {
      if (behaviorSubject.value == defaultObject) {
      API
      .request()
      .onErrorReturn(defaultObject)
      .subscribe(behaviorSubject)
      }
      return behaviorSubject
      }


      But I know that calling subscribe is violating Rx chaining, so I'm trying to figure out how to get rid of that.







      retrofit2 rx-java2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 20:42









      Heath BordersHeath Borders

      20.5k9101195




      20.5k9101195
























          1 Answer
          1






          active

          oldest

          votes


















          0














          thank you for the interesting scenario. Here's a solution which I believe covers your requirements. It is longer than your solution but should be safe when it comes to multiple subscriptions.



          // subscribe to this observable with one or more subscribers
          val requestObservable = replayAndRetry(API.request(), defaultObject)

          private fun <T> replayAndRetry(request: Single<T>, defaultValue: T): Observable<T> {
          val responses = BehaviorSubject.create<T>()

          val initialRequest = request
          .onErrorReturnItem(defaultValue)
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .cache() // run the initial request at most once

          val retryWhenNecessary = Maybe
          .fromCallable { if (responses.value == defaultValue) true else null }
          .flatMapCompletable { request
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .onErrorComplete() // subject already has the default value
          }
          .toObservable<T>().share() // avoid multiple simultaneous retries

          return responses // source for all responses
          .mergeWith(initialRequest) // will run once and then complete
          .mergeWith(retryWhenNecessary) // will check for default item on every subscription
          // will not run simultaneous retries
          }





          share|improve this answer
























          • Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

            – Heath Borders
            Feb 12 at 18:59












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345109%2fcreate-an-observable-that-generates-a-single-on-subscribe-but-which-stops-and-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          thank you for the interesting scenario. Here's a solution which I believe covers your requirements. It is longer than your solution but should be safe when it comes to multiple subscriptions.



          // subscribe to this observable with one or more subscribers
          val requestObservable = replayAndRetry(API.request(), defaultObject)

          private fun <T> replayAndRetry(request: Single<T>, defaultValue: T): Observable<T> {
          val responses = BehaviorSubject.create<T>()

          val initialRequest = request
          .onErrorReturnItem(defaultValue)
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .cache() // run the initial request at most once

          val retryWhenNecessary = Maybe
          .fromCallable { if (responses.value == defaultValue) true else null }
          .flatMapCompletable { request
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .onErrorComplete() // subject already has the default value
          }
          .toObservable<T>().share() // avoid multiple simultaneous retries

          return responses // source for all responses
          .mergeWith(initialRequest) // will run once and then complete
          .mergeWith(retryWhenNecessary) // will check for default item on every subscription
          // will not run simultaneous retries
          }





          share|improve this answer
























          • Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

            – Heath Borders
            Feb 12 at 18:59
















          0














          thank you for the interesting scenario. Here's a solution which I believe covers your requirements. It is longer than your solution but should be safe when it comes to multiple subscriptions.



          // subscribe to this observable with one or more subscribers
          val requestObservable = replayAndRetry(API.request(), defaultObject)

          private fun <T> replayAndRetry(request: Single<T>, defaultValue: T): Observable<T> {
          val responses = BehaviorSubject.create<T>()

          val initialRequest = request
          .onErrorReturnItem(defaultValue)
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .cache() // run the initial request at most once

          val retryWhenNecessary = Maybe
          .fromCallable { if (responses.value == defaultValue) true else null }
          .flatMapCompletable { request
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .onErrorComplete() // subject already has the default value
          }
          .toObservable<T>().share() // avoid multiple simultaneous retries

          return responses // source for all responses
          .mergeWith(initialRequest) // will run once and then complete
          .mergeWith(retryWhenNecessary) // will check for default item on every subscription
          // will not run simultaneous retries
          }





          share|improve this answer
























          • Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

            – Heath Borders
            Feb 12 at 18:59














          0












          0








          0







          thank you for the interesting scenario. Here's a solution which I believe covers your requirements. It is longer than your solution but should be safe when it comes to multiple subscriptions.



          // subscribe to this observable with one or more subscribers
          val requestObservable = replayAndRetry(API.request(), defaultObject)

          private fun <T> replayAndRetry(request: Single<T>, defaultValue: T): Observable<T> {
          val responses = BehaviorSubject.create<T>()

          val initialRequest = request
          .onErrorReturnItem(defaultValue)
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .cache() // run the initial request at most once

          val retryWhenNecessary = Maybe
          .fromCallable { if (responses.value == defaultValue) true else null }
          .flatMapCompletable { request
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .onErrorComplete() // subject already has the default value
          }
          .toObservable<T>().share() // avoid multiple simultaneous retries

          return responses // source for all responses
          .mergeWith(initialRequest) // will run once and then complete
          .mergeWith(retryWhenNecessary) // will check for default item on every subscription
          // will not run simultaneous retries
          }





          share|improve this answer













          thank you for the interesting scenario. Here's a solution which I believe covers your requirements. It is longer than your solution but should be safe when it comes to multiple subscriptions.



          // subscribe to this observable with one or more subscribers
          val requestObservable = replayAndRetry(API.request(), defaultObject)

          private fun <T> replayAndRetry(request: Single<T>, defaultValue: T): Observable<T> {
          val responses = BehaviorSubject.create<T>()

          val initialRequest = request
          .onErrorReturnItem(defaultValue)
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .cache() // run the initial request at most once

          val retryWhenNecessary = Maybe
          .fromCallable { if (responses.value == defaultValue) true else null }
          .flatMapCompletable { request
          .doOnSuccess(responses::onNext)
          .ignoreElement()
          .onErrorComplete() // subject already has the default value
          }
          .toObservable<T>().share() // avoid multiple simultaneous retries

          return responses // source for all responses
          .mergeWith(initialRequest) // will run once and then complete
          .mergeWith(retryWhenNecessary) // will check for default item on every subscription
          // will not run simultaneous retries
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 12 at 16:45









          laengerlaenger

          5401616




          5401616













          • Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

            – Heath Borders
            Feb 12 at 18:59



















          • Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

            – Heath Borders
            Feb 12 at 18:59

















          Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

          – Heath Borders
          Feb 12 at 18:59





          Thanks very much! I need to investigate this further to understand everything. I'm not familiar with mergeWith.

          – Heath Borders
          Feb 12 at 18:59




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345109%2fcreate-an-observable-that-generates-a-single-on-subscribe-but-which-stops-and-r%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine