title: rxSwift Observable dependency
body: <p>I’m new to rxSwift. I have 3 observables, the checkAccount, fetchMails and fetchFolders.<br>
  fetchMails and fetchFolders depend on the checkAccount result. How can I invoke the fetchMails and fetchFolders operation using the UIButton Tap? And if the checkAccount success, I don't what it to run each time I fetchMails and fetchFolders. If checkAccount Failed, I want to retry checkAccount when fetchMails and fetchFolders. How can I To achieve this purpose? and This is my code:</p>
  
  <pre><code>@IBOutlet weak var btn1: UIButton!
  @IBOutlet weak var btn2: UIButton!
  var checkAccountO: Observable&lt;Bool&gt;?
  let bag = DisposeBag()
  
  let fetchO: Observable&lt;[String]&gt; = Observable.create { observer in
          DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
              observer.onNext(["1","2"])
          }
          return Disposables.create()
      }
  
  let fetchFolderO: Observable&lt;[String]&gt;  =  Observable.create { observer in
          DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
              observer.onNext(["folder1","folder2"])
          }
          return Disposables.create()
      }
  override func viewDidLoad() {
      super.viewDidLoad()
  
      fetchFolders().subscribe { (evetn) in
          print("folders \(evetn)")
      }.addDisposableTo(bag)
  
      fetchMails().subscribe { (evetn) in
          print("mails \(evetn)")
      }.addDisposableTo(bag)
  }
  
  func checkAccount() -&gt; Observable&lt;Bool&gt; {
      if let ob = checkAccountO {
          return ob
      }
      checkAccountO = Observable.create { (observer) -&gt; Disposable in
          print("checking...")
          DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1000)) {
              let i = arc4random() % 2
              if i == 0 {
                  print("succ")
                  observer.onNext(true)
                  observer.onCompleted()
              }else {
                  print("failed:\(i)")
                  let err = NSError.init(domain: "err", code: 1001, userInfo: nil)
                  observer.onError(err)
              }
          }
          return Disposables.create()
      }.retry(3).shareReplay(1)
      return checkAccountO!
  }
  
  func fetchMails() -&gt; Observable&lt;[String]&gt; {
      return checkAccount().flatMap({ (_) -&gt; Observable&lt;[String]&gt; in
          return self.fetchO
      })
  }
  
  func fetchFolders() -&gt; Observable&lt;[String]&gt; {
      return checkAccount().flatMap({ (_) -&gt; Observable&lt;[String]&gt; in
          return self.fetchFolderO
      })
  }
  </code></pre>
  
  <p>according to @Timofey Solonin's answer, I change the fetchFoders and fetchMails observable to flatmap from button.rx.tap like this, but still not known how to use retrywhen</p>
  
  <pre><code>    func fetchMails() -&gt; Observable&lt;[String]&gt; {
      let rxtap = btn1.rx.tap
      return rxtap.flatMap { (_) -&gt; Observable&lt;[String]&gt; in
          return self.checkAccount().flatMap({ (_) -&gt; Observable&lt;[String]&gt; in
              return self.fetchO
          })
      }
  
  }
  
  func fetchFolders() -&gt; Observable&lt;[String]&gt; {
      let rxtap = btn2.rx.tap
      return rxtap.flatMap { (_) -&gt; Observable&lt;[String]&gt; in
          return self.checkAccount().flatMap({ (_) -&gt; Observable&lt;[String]&gt; in
              return self.fetchFolderO
          })
      }
  }
  </code></pre>