title: RxJava with android repository pattern
body: <p>I am using <code>RxJava</code> with repository pattern, <code>Room</code> as database and <code>Retrofit</code> for getting data from api. Here is my <code>Dao</code></p>
  
  <pre><code>@Dao
  public interface SubjectDao {
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      void insert(SubjectEntity... subjects);
  
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      void insertSubjectEntities(List&lt;SubjectEntity&gt; subjectEntities);
  
      @Insert(onConflict = OnConflictStrategy.IGNORE)
      long createSubjectIfNotExists(SubjectEntity subject);
  
      @Query("SELECT * FROM "+ DbConstants.SUBJECT_LIST_NAME)
      Single&lt;List&lt;SubjectEntity&gt;&gt; getAllSubjects();
  
  }
  </code></pre>
  
  <p>Api call:</p>
  
  <pre><code>public interface BcsQuestionService {
      @GET("get/bTQwGnFsky?indent=2")
      Observable&lt;List&lt;SubjectModel&gt;&gt; getAllSubject();
  }
  </code></pre>
  
  <p>And here is my repository:</p>
  
  <pre><code>public class SubjectRepo extends BaseRepo {
  
      private static final String TAG = "SubjectRepo";
  
      @Inject
      public SubjectRepo(DataManager dataManager, SubjectService apiService) {
          super(dataManager, apiService);
      }
  
      public Observable&lt;List&lt;SubjectModel&gt;&gt; getSubjectList() {
          return Observable
                  .concatArray(getDbSubjectList(), getApiSubjectList())
                  .observeOn(AndroidSchedulers.mainThread());
      }
  
      public Observable&lt;List&lt;SubjectModel&gt;&gt; getDbSubjectList() {
          return mDataManager.getSubjectList()
                  .filter(new Predicate&lt;List&lt;SubjectEntity&gt;&gt;() {
                      @Override
                      public boolean test(List&lt;SubjectEntity&gt; subjectEntities) throws Exception {
                          return !ListUtils.isEmpty(subjectEntities);
                      }
                  }).map(new Function&lt;List&lt;SubjectEntity&gt;, List&lt;SubjectModel&gt;&gt;() {
                      @Override
                      public List&lt;SubjectModel&gt; apply(List&lt;SubjectEntity&gt; subjectEntities) throws Exception {
                          List&lt;SubjectModel&gt; models = new ArrayList&lt;&gt;();
                          for (SubjectEntity entity: subjectEntities) {
                              SubjectModel model = new SubjectModel();
                              model.setId(entity.getId());
                              model.setName(entity.getName());
                          }
                          return models;
                      }
                  })
                  .subscribeOn(Schedulers.io())
                  .toObservable();
      }
  
      public Observable&lt;List&lt;SubjectModel&gt;&gt; getApiSubjectList() {
          return mApiService.getAllSubject()
                  .doOnNext(new Consumer&lt;List&lt;SubjectModel&gt;&gt;() {
                      @Override
                      public void accept(List&lt;SubjectModel&gt; subjectModels) throws Exception {
                          List&lt;SubjectEntity&gt; entities = new ArrayList&lt;&gt;();
                          for (SubjectModel model: subjectModels) {
                              SubjectEntity entity = new SubjectEntity();
                              entity.setId(model.getId());
                              entity.setName(model.getName());
                          }
                          mDataManager.insertSubjectListEntity(entities);
                      }
                  });
      }
  }
  </code></pre>
  
  <p>Currently I am using <code>concatArray</code> operator to get data from database and api. But Only want to call the api if and only if I get nothing from the database. And I want to save the data into the database. Which operator should I use to server my purpose?</p>
  
  <p>In additional,
  I want to update my view when data is inserted into the database. For that reason I will change this method</p>
  
  <pre><code>@Query("SELECT * FROM "+ DbConstants.SUBJECT_LIST_NAME)
  Single&lt;List&lt;SubjectEntity&gt;&gt; getAllSubjects();
  </code></pre>
  
  <p>into this</p>
  
  <pre><code>@Query("SELECT * FROM "+ DbConstants.SUBJECT_LIST_NAME)
  Flowable&lt;List&lt;SubjectEntity&gt;&gt; getAllSubjects();
  </code></pre>