RxJava: Complete stream based on condition











up vote
0
down vote

favorite












I have on Observable, which emits items when some data is coming from a BLE connection:



public interface CommunicationController {
Flowable<DataContainer> dataReceived();
}


On top of this I want to build a Observable, which completes when one of the following conditions is true:



a. I receive two messages of a specific type (this is done by using filter operator on the received DataContainer item.



communicationController.dataReceived()
.filter(data -> isTypeA(data) || isTypeB(data))
.take(2)
.toList()
.map(dataContainers -> doSomeMappingToCommon object)


b. I receive one message of a specific type (again using filter operator).



communicationController.dataReceived()
.filter(data -> isTypeC(data))
.firstOrError()
.map(dataContainers -> doSomeMappingToCommon object);


How can I combine those two Observables into a single one?
Additionally only one of the two Observables will emit an item.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have on Observable, which emits items when some data is coming from a BLE connection:



    public interface CommunicationController {
    Flowable<DataContainer> dataReceived();
    }


    On top of this I want to build a Observable, which completes when one of the following conditions is true:



    a. I receive two messages of a specific type (this is done by using filter operator on the received DataContainer item.



    communicationController.dataReceived()
    .filter(data -> isTypeA(data) || isTypeB(data))
    .take(2)
    .toList()
    .map(dataContainers -> doSomeMappingToCommon object)


    b. I receive one message of a specific type (again using filter operator).



    communicationController.dataReceived()
    .filter(data -> isTypeC(data))
    .firstOrError()
    .map(dataContainers -> doSomeMappingToCommon object);


    How can I combine those two Observables into a single one?
    Additionally only one of the two Observables will emit an item.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have on Observable, which emits items when some data is coming from a BLE connection:



      public interface CommunicationController {
      Flowable<DataContainer> dataReceived();
      }


      On top of this I want to build a Observable, which completes when one of the following conditions is true:



      a. I receive two messages of a specific type (this is done by using filter operator on the received DataContainer item.



      communicationController.dataReceived()
      .filter(data -> isTypeA(data) || isTypeB(data))
      .take(2)
      .toList()
      .map(dataContainers -> doSomeMappingToCommon object)


      b. I receive one message of a specific type (again using filter operator).



      communicationController.dataReceived()
      .filter(data -> isTypeC(data))
      .firstOrError()
      .map(dataContainers -> doSomeMappingToCommon object);


      How can I combine those two Observables into a single one?
      Additionally only one of the two Observables will emit an item.










      share|improve this question















      I have on Observable, which emits items when some data is coming from a BLE connection:



      public interface CommunicationController {
      Flowable<DataContainer> dataReceived();
      }


      On top of this I want to build a Observable, which completes when one of the following conditions is true:



      a. I receive two messages of a specific type (this is done by using filter operator on the received DataContainer item.



      communicationController.dataReceived()
      .filter(data -> isTypeA(data) || isTypeB(data))
      .take(2)
      .toList()
      .map(dataContainers -> doSomeMappingToCommon object)


      b. I receive one message of a specific type (again using filter operator).



      communicationController.dataReceived()
      .filter(data -> isTypeC(data))
      .firstOrError()
      .map(dataContainers -> doSomeMappingToCommon object);


      How can I combine those two Observables into a single one?
      Additionally only one of the two Observables will emit an item.







      android rx-java2






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 9:46

























      asked Nov 8 at 9:34









      Christopher

      4,12512142




      4,12512142
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          I think your answer will be Observable.merge: http://reactivex.io/documentation/operators/merge.html



          According to the documentation, this does not wait for every observable to emit something.



          One option is to use merge and takeUntil: http://reactivex.io/documentation/operators/takeuntil.html



          Kotlin code:



          val stopStream = Observable.merge(...)

          streamThatWillRun = streamThatWillRun.takeUntil(stopStream)





          share|improve this answer





















          • Yes, thanks a lot!
            – Christopher
            Nov 8 at 11:12


















          up vote
          0
          down vote













          To combine 2 observables, you can use operator zipWith. Example:



          Observable<AppMetaDataBiz> appMetaDataObservable = this.mAppRepository.getAppMetaData();

          Observable<ProductBiz> productDetailsObservable =
          this.mProductRepository.getProductDetails(this.productId);

          return productDetailsObservable.zipWith(appMetaDataObservable,
          ((jmProduct, jmAppMetaData) -> {

          //TODO: implement business logic

          return jmProduct;
          }));





          share|improve this answer








          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
            – Christopher
            Nov 8 at 9:46










          • Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
            – Louis Solo
            Nov 8 at 9:52











          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',
          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%2f53204924%2frxjava-complete-stream-based-on-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          I think your answer will be Observable.merge: http://reactivex.io/documentation/operators/merge.html



          According to the documentation, this does not wait for every observable to emit something.



          One option is to use merge and takeUntil: http://reactivex.io/documentation/operators/takeuntil.html



          Kotlin code:



          val stopStream = Observable.merge(...)

          streamThatWillRun = streamThatWillRun.takeUntil(stopStream)





          share|improve this answer





















          • Yes, thanks a lot!
            – Christopher
            Nov 8 at 11:12















          up vote
          3
          down vote



          accepted










          I think your answer will be Observable.merge: http://reactivex.io/documentation/operators/merge.html



          According to the documentation, this does not wait for every observable to emit something.



          One option is to use merge and takeUntil: http://reactivex.io/documentation/operators/takeuntil.html



          Kotlin code:



          val stopStream = Observable.merge(...)

          streamThatWillRun = streamThatWillRun.takeUntil(stopStream)





          share|improve this answer





















          • Yes, thanks a lot!
            – Christopher
            Nov 8 at 11:12













          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          I think your answer will be Observable.merge: http://reactivex.io/documentation/operators/merge.html



          According to the documentation, this does not wait for every observable to emit something.



          One option is to use merge and takeUntil: http://reactivex.io/documentation/operators/takeuntil.html



          Kotlin code:



          val stopStream = Observable.merge(...)

          streamThatWillRun = streamThatWillRun.takeUntil(stopStream)





          share|improve this answer












          I think your answer will be Observable.merge: http://reactivex.io/documentation/operators/merge.html



          According to the documentation, this does not wait for every observable to emit something.



          One option is to use merge and takeUntil: http://reactivex.io/documentation/operators/takeuntil.html



          Kotlin code:



          val stopStream = Observable.merge(...)

          streamThatWillRun = streamThatWillRun.takeUntil(stopStream)






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 8 at 10:41









          Leandro Ocampo

          694515




          694515












          • Yes, thanks a lot!
            – Christopher
            Nov 8 at 11:12


















          • Yes, thanks a lot!
            – Christopher
            Nov 8 at 11:12
















          Yes, thanks a lot!
          – Christopher
          Nov 8 at 11:12




          Yes, thanks a lot!
          – Christopher
          Nov 8 at 11:12












          up vote
          0
          down vote













          To combine 2 observables, you can use operator zipWith. Example:



          Observable<AppMetaDataBiz> appMetaDataObservable = this.mAppRepository.getAppMetaData();

          Observable<ProductBiz> productDetailsObservable =
          this.mProductRepository.getProductDetails(this.productId);

          return productDetailsObservable.zipWith(appMetaDataObservable,
          ((jmProduct, jmAppMetaData) -> {

          //TODO: implement business logic

          return jmProduct;
          }));





          share|improve this answer








          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
            – Christopher
            Nov 8 at 9:46










          • Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
            – Louis Solo
            Nov 8 at 9:52















          up vote
          0
          down vote













          To combine 2 observables, you can use operator zipWith. Example:



          Observable<AppMetaDataBiz> appMetaDataObservable = this.mAppRepository.getAppMetaData();

          Observable<ProductBiz> productDetailsObservable =
          this.mProductRepository.getProductDetails(this.productId);

          return productDetailsObservable.zipWith(appMetaDataObservable,
          ((jmProduct, jmAppMetaData) -> {

          //TODO: implement business logic

          return jmProduct;
          }));





          share|improve this answer








          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
            – Christopher
            Nov 8 at 9:46










          • Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
            – Louis Solo
            Nov 8 at 9:52













          up vote
          0
          down vote










          up vote
          0
          down vote









          To combine 2 observables, you can use operator zipWith. Example:



          Observable<AppMetaDataBiz> appMetaDataObservable = this.mAppRepository.getAppMetaData();

          Observable<ProductBiz> productDetailsObservable =
          this.mProductRepository.getProductDetails(this.productId);

          return productDetailsObservable.zipWith(appMetaDataObservable,
          ((jmProduct, jmAppMetaData) -> {

          //TODO: implement business logic

          return jmProduct;
          }));





          share|improve this answer








          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          To combine 2 observables, you can use operator zipWith. Example:



          Observable<AppMetaDataBiz> appMetaDataObservable = this.mAppRepository.getAppMetaData();

          Observable<ProductBiz> productDetailsObservable =
          this.mProductRepository.getProductDetails(this.productId);

          return productDetailsObservable.zipWith(appMetaDataObservable,
          ((jmProduct, jmAppMetaData) -> {

          //TODO: implement business logic

          return jmProduct;
          }));






          share|improve this answer








          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 8 at 9:40









          Louis Solo

          1062




          1062




          New contributor




          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Louis Solo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
            – Christopher
            Nov 8 at 9:46










          • Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
            – Louis Solo
            Nov 8 at 9:52


















          • Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
            – Christopher
            Nov 8 at 9:46










          • Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
            – Louis Solo
            Nov 8 at 9:52
















          Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
          – Christopher
          Nov 8 at 9:46




          Hmm, zipWith will not solve my problem, as it will only emit one event if both Observables emit one event. In my case it's more that only one of the two observables will emit one item. I should clarify this in the question.
          – Christopher
          Nov 8 at 9:46












          Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
          – Louis Solo
          Nov 8 at 9:52




          Hmm, I may get your idea. If I'm right, combineLatest may help you: reactivex.io/documentation/operators/combinelatest.html
          – Louis Solo
          Nov 8 at 9:52


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204924%2frxjava-complete-stream-based-on-condition%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)