title: RxJava - Trigger another Observable/Completable before complete and complete only after it
body: <p>I am not completely new to RxJava, but I am blocked with what seems to be a simple task.</p>
  
  <p>I have a datasource that exposes a reactive API, all I want to do is to fetch some data, return it and close automatically the connection when there is nothing else to emit.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public Observable&lt;Object&gt; execute(String query) {
  
      Single&lt;RxConnection&gt; rxConnection = getRxDB().getConnection();
  
      return rxConnection.flatMapObservable(conn -&gt; {
          Observable&lt;Object&gt; rxResult = conn.query(query);
  
          return rxResult.doOnCompleted(() -&gt; {
              conn.close(); // THIS DOES NOT WORK. I would like to close the connection and to wait without blocking.
          });
  
      });
  
  }
  </code></pre>
  
  <p>conn.query() and conn.close() are asynchronously executed in different Schedulers.
  This code does not work because conn.close() returns a Completable which has no subscribers. In addition, if I manually subscribe in the doOnCompleted method itself, the rxResult Observable completes without waiting for the connection to be closed.</p>
  
  <p>I would like that the "execute(String query)" method returns an Observable that:
  - Emit all the items fetched by the conn.query() call
  - when there are no more items to emit, it triggers conn.close()
  - completes only AFTER the conn.close() Completable;</p>
  
  <p>Thanks.</p>