title: RxSwift: tap a button to do a request with Alamofire, how to make it work?
body: <p>I have some UI rx button tap, I want to connect it to the network rx part.</p>
  <p>Here is Rx btn tap part:</p>
  <pre><code>btn.rx.tap.take(1).flatMap {  [unowned self]  () -&gt; Observable&lt;Bool&gt;  in
              // ...
              return NetManager.standard.request()
          }.debug().do(onNext: { _ in
                  print(&quot;The data flows here&quot;)
              }).flatMapLatest { (echo: Bool) -&gt; Observable&lt;Bool&gt; in
              if echo{
                  if let app = UIApplication.shared.delegate as? AppDelegate{
                      app.goHome()
                  }
                  return Observable.of(true)
              }
              else{
                  return Observable.of(false)
              }
          }.subscribe(onNext: { (result) in
              // ... , do sth with result
          }).disposed(by: rx.disposeBag)
  </code></pre>
  <p>Here is net request part with Alamofire:</p>
  <pre><code>class NetManager: NSObject {
      
      static let standard = NetManager()
      var dataRequest: DataRequest?
      
      func request() -&gt; Observable&lt;Bool&gt; {
             return Observable&lt;Bool&gt;.create{ (observer) -&gt; Disposable in
                      let data = // ...
                      // ...
                      self.dataRequest = AF.request(aURL, method: HTTPMethod.post, parameters: data, encoder: JSONParameterEncoder.prettyPrinted, headers: aHead).response { (response: DataResponse&lt;Data?&gt;) in
                              self.hud?.hide()
                             if let data = response.data{
                                 observer.onNext(true)
                                  // ....
                             }
                             else{
                                 observer.onNext(false)
                             }
                         }
                      observer.onCompleted()
                      return Disposables.create{}
             }
         }
      
  }
  </code></pre>
  <p>Here is the debug info:</p>
  <blockquote>
  <p>Controller.swift:141 (viewDidLoad()) -&gt; subscribed</p>
  <p>Controller.swift:141 (viewDidLoad()) -&gt; Event completed</p>
  <p>Controller.swift:141 (viewDidLoad()) -&gt; isDisposed</p>
  </blockquote>
  <p>The result is , the <code>do( next )</code> part seems like that it has not been called. Because no <code>print</code> in console.</p>
  <hr />
  <p>The question is, <strong>how to make the rx logic work?</strong></p>
  <hr />
  <p>Here is what I tried:</p>
  <p>turn <code>btn.rx.tap.flatMap </code> to <code>btn.rx.tap.take(1).flatMap </code></p>
  <p>because from <a href="https://github.com/ReactiveX/RxSwift/issues/1939" rel="nofollow noreferrer">this issue</a></p>
  <blockquote>
  <p>if a flatMap completion would terminate the outer stream, it would mean that no more button taps would be registered in your stream, which don't really make sense.</p>
  </blockquote>
  <hr />
  <p>Seen from <a href="https://stackoverflow.com/questions/33968378/how-to-transform-rx-tap-of-uibutton-to-a-network-request-directly-without-sendin">this question</a></p>
  <pre><code>  btn.rx.tap.map { () -&gt; Observable&lt;Bool&gt; in
  
      return NetManager.standard.request()
    }.flatMapLatest { (observer: Observable&lt;Bool&gt;) -&gt; Observable&lt;Bool&gt; in
    
      // of cource, I can subscribe to observer
      /*
       observer.subscribe(onNext: { (echo) in
           
       })
       */
      
      
      // how to get the event element elegantly.
      
      return Observable.of(false)
  }.subscribe(onNext: { (_) in
      
  }).disposed(by: rx.disposeBag)
  </code></pre>
  <p>how to do this more elegantly.</p>