title: How to pause/resume an Observable?
body: <p>I am trying to implement paging with retrofit, but I am struggling to find how to pause an observable so that it does not continue requesting pages that are not needed.</p>
  
  <p>Basic question is: can I tell an observable source to "pause" and "resume"?  I'm not talking about buffering or skipping, but rather I would like for the source observable to stop completely, ie: don't make any web requests, etc.</p>
  
  <p>Below is some simulated code that I'm working with.  rangeObservable is the simulated webserver "pager", and timerObservable would be like receiving scroll events.</p>
  
  <pre><code>package example.wanna.be.pausable;
  
  import java.io.IOException;
  import java.lang.Throwable;
  import java.util.concurrent.TimeUnit;
  
  import rx.Observable;
  import rx.observables.ConnectableObservable;
  import rx.Subscription;
  import rx.Subscriber;
  
  public class Main {
  
    private static ConnectableObservable rangeObservable;
  
    private static void setPaused(boolean paused) {
      // How do I pause/resume rangeObservable?
    }
  
    public static void main(String[] args) {
  
      rangeObservable = Observable.range(0, Integer.MAX_VALUE).publish();
      Observable timerObservable = Observable.timer(2, 2, TimeUnit.SECONDS);
  
      rangeObservable.subscribe(new Subscriber&lt;Integer&gt;() {
  
        private int count = 0;
  
        public void onStart() {
          System.out.println("Range started");
        }
  
        public void onNext(Integer i) {
          System.out.println("Range: " + i);
  
          if (++count % 20 == 0) {
            System.out.println("Pausing");
            setPaused(true);
          }
        }
  
        public void onError(Throwable e) {
          e.printStackTrace();
        }
  
        public void onCompleted() {
          System.out.println("Range done");
        }
  
      });
  
      timerObservable.subscribe(new Subscriber&lt;Long&gt;() {
  
        public void onStart() {
          System.out.println("Time started");
  
          // I dont know where to put this
          // rangeObservable.connect();
        }
  
        public void onNext(Long i) {
          System.out.println("Timer: " + i);
          setPaused(false);
        }
  
        public void onError(Throwable e) {
          e.printStackTrace();
        }
  
        public void onCompleted() {
          System.out.println("Timer done");
        }
  
      });
  
      // for some reason I have to do this or it just exits immediately
      try {
        System.in.read();
      } catch(IOException e) {
        e.printStackTrace();
      }
  
    }
  
  }
  </code></pre>
  
  <p>Any guidance is appreciated!</p>