title: How best to setup Room and Retrofit
body: <p>I'm currently trying to setup Room with Retrofit. I have a working implementation...</p>
  
  <p>It requests an <code>ApiItemBean</code>, then the <code>ApiDatabase</code> calls its <code>ApiItemDAO</code> to map a databased or new <code>ApiItem</code> with the <code>ApiItemBean</code> into a <code>Single&lt;ApiItem&gt;</code>.</p>
  
  <p>That single is used in the <code>ApiRepository</code> by the <code>ApiRequest</code>, and eventually subscribed to in the figurative <code>Usage</code>.</p>
  
  <p><strong>ApiItemBean</strong></p>
  
  <pre><code>  public class ApiItemBean {
      private final int a;
      private final int b;
      public ApiItemBean(int a, int b) {
        this.a = a;
        this.b = b;
      }
      public int a() {
        return a;
      }
      public int b() {
        return b;
      }
    }
  </code></pre>
  
  <p><strong>ApiItem</strong></p>
  
  <pre><code>  @Entity(tableName = "api_item") public static class ApiItem {
      @PrimaryKey private final int id;
      ApiItem(int id) {
        this.id = id;
      }
      public int id() {
        return id;
      }
    }
  </code></pre>
  
  <p><strong>ApiItemDAO</strong></p>
  
  <pre><code>  @Dao public interface ApiItemDAO {
      @Insert(onConflict = OnConflictStrategy.REPLACE) long insert(ApiItem apiItem);
      @Query("SELECT * FROM api_item WHERE id = :id") ApiItem apiItem(int id);
    }
  </code></pre>
  
  <p><strong>ApiDatabase</strong></p>
  
  <pre><code>  @Database(entities = { ApiItem.class }, version = 1) abstract class ApiDatabase extends RoomDatabase {
      abstract ApiItemDAO apiItemDAO();
      // More...
  
      Single&lt;ApiItem&gt; mapApiItem(ApiItemBean apiItemBean) {
        final ApiItem apiItem = new ApiItem(apiItemBean.a());
        final ApiItem apiItemDb = apiItemDAO().apiItem(apiItem.id());
        if (apiItemDb == null || !apiItemDb.equals(apiItem))
          Log.i(getClass().getSimpleName(), String.format("insert=%s", apiItemDAO().insert(apiItem)));
        return Single.just(apiItemDAO().apiItem(apiItem.id()));
      }
      // More...
    }
  </code></pre>
  
  <p><strong>ApiRequest</strong></p>
  
  <pre><code>  public interface ApiRequest {
      @GET("API_ITEM_BEAN_REQUEST") Single&lt;ApiItemBean&gt; getApiItemBean();
      // More...
    }
  </code></pre>
  
  <p><strong>ApiRepository</strong></p>
  
  <pre><code>  public class ApiRepository {
      private final ApiRequest apiRequest;
      private final ApiDatabase apiDatabase;
      ApiRepository(ApiRequest apiRequest, ApiDatabase apiDatabase) {
        this.apiRequest = apiRequest;
        this.apiDatabase = apiDatabase;
      }
      Single&lt;ApiItem&gt; getApiItem() {
        return apiRequest.getApiItemBean().flatMap(apiDatabase::mapApiItem);
      }
      // More...
    }
  </code></pre>
  
  <p><strong>Usage</strong></p>
  
  <pre><code>  public class Usage {
      private final ApiRepository apiRepository;
      Usage(ApiRepository apiRepository) {
        this.apiRepository = apiRepository;
      }
      public void onUse() {
        apiRepository.getApiItem().subscribe();
      }
    }
  </code></pre>
  
  <p>I am unsure on how best to check the requests against the database. See <code>ApiDatabase::mapApiItem</code> for an example of how I thought to do it.</p>
  
  <p>Side note: I know there is no specific question, and should possibly be moved to CodeReview, however the code example is not fully working considering it would need to depend on a specific api and I thought it best to try here first. Sorry to any mods.</p>