title: RxCocoa - prevent multiple view controller pushes when there's lag
body: <p>As is the case with both Reactive and non-Reactive iOS projects alike, if you have a UI element (e.g. a button or a table view cell that is being selected) that pushes a view controller onto the navigation stack, if there's lag for some reason (especially on older devices) repetitive taps can result in duplicate pushes and thus bad UX. </p>
  
  <p>Normally you could disable the element after the first tap.</p>
  
  <p>For example:</p>
  
  <pre><code>@IBAction func myButtonTap() { 
      button.isEnabled = false
      doTheRestOfTheAction()
  }
  </code></pre>
  
  <p>I am relatively new to RxSwift. I am trying to figure out an appropriate Reactive way to implement this to fix a few bugs in my app where views get pushed repetitively.</p>
  
  <p>Some thoughts:</p>
  
  <p>Could use <code>debounce</code> or <code>throttle</code> but seems like a bandaid and won't necessarily fix every situation.</p>
  
  <p>I'm currently thinking that the best way is to dispose of the subscription once the expected event has occurred.</p>
  
  <pre><code>let disposable = tableView.rx.itemSelected
      .subscribe(onNext: { [weak self] indexPath in 
          self?.performSegue(withIdentifier: "MySegueIdentifier", sender: self)
      })
  
  ...
  
  func prepareForSegue() {
      myDisposable.dispose()
      finishPrepareForSegue()
  }
  </code></pre>
  
  <p>Although if you want to unsubscribe inside the subscribe block, the compiler complains about using a variable inside its own initial value, which makes sense. I suppose there are workarounds but I wonder, is there a better way? Maybe a Reactive operator I'm missing? </p>
  
  <p>Tried searching around for similar examples but the results were limited.</p>
  
  <p>Thanks</p>
  
  <p>EDIT: perhaps the <code>takeUntil</code> operator? </p>
  
  <p><a href="https://stackoverflow.com/a/35912089/1032372">Possibly relevant SO question</a>.</p>