title: Keep executing an observable in repository layer when UseCase is disposed
body: <p>What is the proper way to execute some kind of "background" action, like saving data in the database, when this action is triggered by a presenter, but the presenter is disposing the observable before the end of the query?</p>
  
  <p>Should I decouple my Repository observable from the UseCase one?</p>
  
  <h3>Example</h3>
  
  <p>I'me developing an Android application following the Clean Architecture and MVP patterns, using RxJava and Dagger.</p>
  
  <p>In a Dialog, I'm listing devices, and can connect to one when selecting it in the list. 
  When an element in the list is clicked, the dialog is dismissed, and I'm setting/saving this device as the 'current' device in my repository layer and animating a logo in the toolbar of the activity (somehow like the Chromecast is doing).</p>
  
  <p>In my presenter, I dispose my UseCases when the view is detached, so if the saving is not complete when the dialog is dismissed, the observable gets disposed and the device is not set as 'current' device.</p>
  
  <h3>Presenter</h3>
  
  <pre><code>@Override
  public void deviceClicked(String id) {
      getMvpView().dismissView();
      mConnectToDeviceUseCase.execute(id, new DisposableCompletableObserver() {
          @Override
          public void onComplete() {
              Timber.d("Connected to device");
          }
  
          @Override
          public void onError(Throwable e) {
              Timber.e("Error while connecting to device: %s", e.getMessage());
          }
      });
  }
  </code></pre>
  
  <h3>UseCase</h3>
  
  <pre><code>public class ConnectToDeviceUseCase extends UseCaseCompletableWithParameter&lt;String, DevicesRepository&gt; {
  
      @Inject
      public ConnectToDeviceUseCase(DevicesRepository DevicesRepository,
                                     @Named("Thread") Scheduler threadScheduler,
                                     @Named("PostExecution") Scheduler postExecutionScheduler) {
          super(devicesRepository, threadScheduler, postExecutionScheduler);
      }
  
      @Override
      protected Completable buildObservable(String id) {
          Timber.d("GetDevicesUseCase buildObservable");
  
          return repository.connectToDevice(id);
      }
  }
  </code></pre>
  
  <h3>Repository</h3>
  
  <pre><code>private ReplaySubject&lt;DeviceConnection&gt; connectionStatus = ReplaySubject.create();
  </code></pre>
  
  <p>...</p>
  
  <pre><code>@Override
  public Completable connectToDevice(String id) {
      Timber.d("connectToDevice IN");
      return mDLNADataSource.getDevices()
              .flatMapIterable(items -&gt; items)
              .filter(item -&gt; item.id().equals(id))
              .firstOrError()
              .flatMapCompletable(this::saveAsCurrent)
              .doOnSubscribe(a -&gt;
                      {
                          Timber.d("connectToDevice doOnSubscribe");
                          connectionStatus.onNext(DeviceConnection.builder().setStatus(DeviceConnection.STATUS_CONNECTING).build());
                      }
              )
              .doOnComplete(() -&gt;
                      {
                          Timber.d("connectToDevice doOnComplete");
                          connectionStatus.onNext(DeviceConnection.builder().setStatus(DeviceConnection.STATUS_CONNECTED).build());
                      }
              )
              .doOnError(a -&gt;
                      {
                          Timber.d("connectToDevice doOnError");
                          connectionStatus.onNext(DeviceConnection.builder().setStatus(DeviceConnection.STATUS_NOT_CONNECTED).build());
                      }
              )
              .doOnDispose(() -&gt;
              {
                  Timber.d("connectToDevice doOnDispose");
              });
  
  }
  </code></pre>