title: RxSwift - How to retry when an observable changes but emit only last value?
body: <p>I'm trying to make chat services chain with RxSwift.
  Basically it should do:</p>
  
  <ul>
  <li>Check if chat has id, if yes go to next, if no flatMap to createChat Service</li>
  <li>Check if sockect connection is Enabled</li>
  <li>Send Chat Message</li>
  </ul>
  
  <p>Basically function below works, but it is not checking socket connection status for now. 
  It should check before second flatMap if sockets are connected, if yes it should go to flatMap with messageUpload if not it should wait until connection returns true. 
  I have Variable with is telling me about current connection status(Bool)</p>
  
  <pre><code>chatSocketService.isSubscribedToChannel.asObservable()
  </code></pre>
  
  <p>but I can't figure out how to put them together. 
  I tried with third flatMap(before current last one) it doesn't work.
  Next problem is that user can try to send few messages before connection is back so each time he hits sendButton this method is executed, so it should send only last message when connection is back.
  Any idea how I can handle that with Rx?</p>
  
  <pre><code>func sendMessage(withBody body: String) {
      guard !body.isEmpty else { return }
  
      Observable.just(chatModel.value)
          .filter({ $0.product != nil })
          .flatMap({ [unowned self] chatModel -&gt; Observable&lt;ChatModel&gt; in
              if chatModel.id != nil {
                  return Observable.just(chatModel)
              } else {
                  return self.createChat(withProductModel: chatModel.product!)
              }
          })
          .flatMap({ [unowned self] chatModel -&gt; Observable&lt;ChatMessageModel&gt; in
              return self.chatService.uploadChatMessage(forChat: chatModel, withBody: body)
                      .trackActivity(self.progressHelper.activityIndicator)
          })
          .subscribe(onNext: { [unowned self] chatMessageModel in
              self.finishedSendingMessage.onNext(())
          })
          .addDisposableTo(disposeBag)
  }
  </code></pre>