title: How can I perform LiveData transformations on a background thread?
body: <p>I have a need to transform one type of data, returned by a <code>LiveData</code> object, into another form <strong>on a background thread</strong> to prevent UI lag.</p>
  
  <p>In my specific case, I have:</p>
  
  <ul>
  <li><code>MyDBRow</code> objects (POJOs consisting of primitive <code>long</code>s and <code>String</code>s);</li>
  <li>a <a href="https://developer.android.com/topic/libraries/architecture/room.html" rel="noreferrer">Room</a> <a href="https://developer.android.com/training/data-storage/room/accessing-data.html" rel="noreferrer">DAO</a> instance emitting these via a <code>LiveData&lt;List&lt;MyDBRow&gt;&gt;</code>; and</li>
  <li>a UI expecting richer <code>MyRichObject</code> objects (POJOs with the primitives inflated into e.g. <a href="http://www.threeten.org/threetenbp/apidocs/index.html" rel="noreferrer">date/time objects</a>)</li>
  </ul>
  
  <p>so I need to transform my <code>LiveData&lt;List&lt;MyDBRow&gt;&gt;</code> into a <code>LiveData&lt;List&lt;MyRichObject&gt;&gt;</code>, but <strong>not on the UI thread</strong>.</p>
  
  <p>The <a href="https://developer.android.com/reference/android/arch/lifecycle/Transformations.html#map(android.arch.lifecycle.LiveData%3CX%3E,%20android.arch.core.util.Function%3CX,%20Y%3E)" rel="noreferrer"><code>Transformations.map(LiveData&lt;X&gt;, Function&lt;X, Y&gt;)</code></a> method does this needed transformation, but I can't use this because it <strong>executes the transformation on the main thread</strong>:</p>
  
  <blockquote>
    <p>Applies the given function on the main thread to each value emitted by <code>source</code> LiveData and returns LiveData, which emits resulting values.</p>
    
    <p>The given function <code>func</code> will be executed on the main thread.</p>
  </blockquote>
  
  <p>What is a clean way to make <code>LiveData</code> transformations occur:</p>
  
  <ol>
  <li>somewhere off the main thread, and</li>
  <li>only as needed (i.e. only when something is observing the intended transformation)?</li>
  </ol>