title: RxJava - When and why to use Observable.share()
body: <p>I've seen patterns like this:</p>
  
  <pre><code>Observable&lt;String&gt; nameChanges = nameDataSource.changes().share();
  
  // One subscriber
  autoUnsubscribe(nameChanges.subscribe(() -&gt; { ... }));
  
  // Another subscriber
  autoUnsubscribe(nameChanges.map(...).filter(...).subscribe(...));
  
  // autoUnsubscribe is called when the UI is torn down
  </code></pre>
  
  <p>My question is:</p>
  
  <p><em>Why is it necessary to call <code>share()</code> whenever I want to listen to the Observable in multiple places?</em></p>
  
  <hr>
  
  <p>Why is not <code>share()</code> the default behavior for all observables?</p>
  
  <p>It would be nice if the code above worked the same even without <code>.share()</code>. I would not have to think about when I need to share an Observable, and when I don't.</p>
  
  <p>Is it for performance reasons because having a single subscriber is a special case that can be handled more efficiently?</p>
  
  <p>From the docs this is not clear to me:</p>
  
  <blockquote>
    <p>share() returns a new ObservableSource that multicasts (shares) the original ObservableSource. As long there is at least one Observer this ObservableSource will be subscribed and emitting data.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/hk2w4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hk2w4.png" alt="enter image description here"></a></p>