title: how to connect working steps using RxSwift
body: <pre><code>class MainViewController: UIViewController {
      let button = UIButton()
      let mainViewModel = MainViewModel()
      let disposeBag = DisposeBag()
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          view.addSubview(button)
          button.frame = CGRect(x: 50, y: 50, width: 100, height: 30)
          
          bind()
      }
      
      func bind() {
          button.rx.tap
              .bind(to: mainViewModel.doAction)
              .disposed(by: disposeBag)
          
          mainViewModel.somethingDoneObservable
              .observeOn(MainScheduler.instance)
              .flatMapLatest { [weak self] value -&gt; Observable&lt;String&gt; in
                  print(value)
                  
                  let vc = Work1ViewController()
                  
                  self?.present(vc, animated: true, completion: nil)
                  
                  return vc.work1ViewModel.work1DoneObservable
              }
              .flatMapLatest { [weak self] value -&gt; Observable&lt;String&gt; in
                  print(value)
  
                  let vc = Work2ViewController()
  
                  self?.present(vc, animated: true, completion: nil)
  
                  return vc.work2ViewModel.work2DoneObservable
              }
              .subscribe(onNext: { newValue in
                  // do something
                  // all done
                  print(newValue)
              })
              .disposed(by: disposeBag)
      }
  }
  
  class MainViewModel {
      let doAction = PublishSubject&lt;Void&gt;()
      let somethingDoneObservable = PublishSubject&lt;String&gt;()
      let disposeBag = DisposeBag()
      
      init() {
          doAction
              .subscribe(onNext: {
                  // do something
                  let when = DispatchTime.now() + DispatchTimeInterval.milliseconds(5000)
                  
                  DispatchQueue.main.asyncAfter(deadline: when) {
                      self.somethingDoneObservable.onNext(&quot;Something Done!!&quot;)
                  }
              })
              .disposed(by: disposeBag)
      }
  }
  
  // ----------------------------------------------------------------------
  
  class Work1ViewController: UIViewController {
      let button = UIButton()
      let work1ViewModel = Work1ViewModel()
      let disposeBag = DisposeBag()
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          view.addSubview(button)
          button.frame = CGRect(x: 50, y: 50, width: 100, height: 30)
          
          bind()
      }
      
      func bind() {
          button.rx.tap
              .bind(to: work1ViewModel.doAction)
              .disposed(by: disposeBag)
  
          work1ViewModel.work1DoneObservable
              .subscribe(onNext: { [weak self] _ in
                  self?.dismiss(animated: false, completion: nil)
              })
              .disposed(by: disposeBag)
      }
  }
  
  class Work1ViewModel {
      let doAction = PublishSubject&lt;Void&gt;()
      let work1DoneObservable = PublishSubject&lt;String&gt;()
      let disposeBag = DisposeBag()
      
      init() {
          doAction
              .subscribe(onNext: {
                  // do work1
                  let when = DispatchTime.now() + DispatchTimeInterval.milliseconds(5000)
                  
                  DispatchQueue.main.asyncAfter(deadline: when) {
                      self.work1DoneObservable.onNext(&quot;Work1 Done!!&quot;)
                  }
              })
              .disposed(by: disposeBag)
      }
  }
  </code></pre>
  <p>(Work1, Work2ViewController can be dismissed without doAction event.)</p>
  <p>as you see, I want to connet several work steps.(work1 -&gt; work2 -&gt; mainVc)</p>
  <p>in this case, I used flatMapLatest.</p>
  <ol>
  <li><p>is it correct way? if not, is there common way or other elegant way?</p>
  </li>
  <li><p>if it is correct, is there any problem?(memory leak or ..)</p>
  </li>
  <li><p>if I tap MainViewController'button later, is it ok?</p>
  </li>
  </ol>