title: Fetching data with retrofit2 and saving in room
body: <p>I am using retrofit2 for fetching data from the server and after fetching saving data in room database and then showing in recycler view. But it is no displayed (my data what I get using retrofit). I try display it in my fragment. In file ...data/data/databese/source.db these data are saved. I see it. So, that means that my code works. But I can't understand why it is not displayed.
  my database class:</p>
  <pre><code>@Database(entities = {Source.class}, exportSchema = false, version = 1)
  public abstract class SourceDatabase extends RoomDatabase {
  
      private static final String DB_NAME = &quot;source.db&quot;;
      public abstract SourceDao sourceDao();
      private static SourceDatabase instance;
  
      public static SourceDatabase getInstance(Context context) {
          if (instance == null) {
              instance =buildDatabaseInstance(context);
          }
          return instance;
      }
      private static SourceDatabase buildDatabaseInstance(Context context) {
          return Room.databaseBuilder(context,
                  SourceDatabase.class,
                  DB_NAME).build();
      }
  }
  </code></pre>
  <p>repository:</p>
  <pre><code>public class DataBaseRepository {
      private static DataBaseRepository  dataBaseRepository;
      private SourceDao sourceDao;
      private LiveData&lt;List&lt;Source&gt;&gt; allSourcestoDb;
      private Context context;
  
      public static DataBaseRepository getInstance(Context context) {
          if (dataBaseRepository == null) {
              dataBaseRepository = new DataBaseRepository(context);
          }
          return dataBaseRepository;
      }
  
      public DataBaseRepository(Context context) {
          this.context = context;
          SourceDatabase db = SourceDatabase.getInstance(context);
          sourceDao = db.sourceDao();
          allSourcestoDb = sourceDao.getSources();
      }
  
      public void getSourceListTodb(String key) {//отправка данных в LiveData
          RestClient restClient = RestClient.getInstance();
          restClient.startRetrofit();
  
          restClient.getServerApi().getNews(key).enqueue(new Callback&lt;News&gt;() {
              @Override
              public void onResponse(Call&lt;News&gt; call, Response&lt;News&gt; response) {
  
                  Completable.fromAction(new Action (){
                      @Override
                      public void run() throws Exception {
                          if (response.body() != null) {
  
                              List&lt;Source&gt; list = response.body().getSources();
                              sourceDao.insert(list);
                          }
                      }
                  }).subscribeOn(Schedulers.io())
                          .observeOn(AndroidSchedulers.mainThread())
                          .subscribe(new CompletableObserver() {
                              @Override
                              public void onSubscribe(Disposable d) {
  
                              }
  
                              @Override
                              public void onComplete() {
                              }
  
                              @Override
                              public void onError(Throwable e) {
  
                              }
                          });
              }
  
              @Override
              public void onFailure(Call&lt;News&gt; call, Throwable t) {
                  Log.d(&quot;error&quot;, &quot;Can't parse data &quot; + t);
              }
          });
      }
  
      public LiveData&lt;List&lt;Source&gt;&gt; getAllSourcestoDb() {
          return allSourcestoDb;
      }
  }
  </code></pre>
  <p>dao:</p>
  <pre><code>@Dao
  public interface SourceDao {
      @Insert(onConflict = OnConflictStrategy.REPLACE)
      void insert(List&lt;Source&gt; sources);
  
      @Query(&quot;SELECT * FROM source&quot;)
      LiveData&lt;List&lt;Source&gt;&gt; getSources();
  }
  </code></pre>
  <p>viewModel:</p>
  <pre><code>public class SourceViewModel extends AndroidViewModel {
      private DataBaseRepository dataBaseRepository;
      private LiveData&lt;List&lt;Source&gt;&gt; allSources; //for db
  
      public SourceViewModel(@NonNull Application application) {
          super(application);
          dataBaseRepository =DataBaseRepository.getInstance(application); //for db
          allSources = dataBaseRepository.getAllSourcestoDb();
      }
  
      public LiveData&lt;List&lt;Source&gt;&gt; getAllSources() {
          return allSources;
      }
  }
  </code></pre>
  <p>and fragment:</p>
  <pre><code>public class SavedDataFragment extends Fragment {
      private SourceViewModel sourceViewModel;
      private DataBaseRepository dataBaseRepository;
      private RecyclerView recyclerView;
      private List&lt;Source&gt; sourceList;
      private SavedDataAdapter adapter;
  
      public SavedDataFragment() {
      }
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.saved_data,container,false);
  
          DataSharedPreference sharedPreference = DataSharedPreference.getSPInstance();
          String api_key = sharedPreference.loadText(getActivity());
  
          dataBaseRepository = new DataBaseRepository(getActivity());
          sourceViewModel = ViewModelProviders.of(this).get(SourceViewModel.class);
  
          recyclerView = view.findViewById(R.id.recyclerViewSavedFragment);
          recyclerView.setHasFixedSize(true);
          recyclerView.setLayoutManager(new LinearLayoutManager(view.getContext()));
  
          sourceList = new ArrayList&lt;&gt;();
  
          adapter = new SavedDataAdapter(getActivity(), sourceList);
          recyclerView.setAdapter(adapter);
  
          sourceViewModel.getAllSources().observe(this, new Observer&lt;List&lt;Source&gt;&gt;() {
              @Override
              public void onChanged(List&lt;Source&gt; sources) {
                adapter.setSourceList(sourceList);
              }
          });
          dataBaseRepository.getSourceListTodb(api_key);
          return view;
      }
  
  }
  </code></pre>
  <p>adapter:</p>
  <pre><code>public class SavedDataAdapter extends RecyclerView.Adapter&lt;SavedDataAdapter.SourceSavedViewHolder&gt; {
      private LayoutInflater inflater;
      private List&lt;Source&gt; sources;
  
      public SavedDataAdapter(Context context, List&lt;Source&gt; sources) {
          this.sources = sources;
          this.inflater = LayoutInflater.from(context);
      }
  
      @NonNull
      @Override
      public SavedDataAdapter.SourceSavedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
          View view = inflater.inflate(R.layout.saved_item, parent, false);
          return new SourceSavedViewHolder(view);
      }
  
      @Override
      public void onBindViewHolder(@NonNull final SavedDataAdapter.SourceSavedViewHolder holder, int position) {
          final Source source = sources.get(position);
          holder.sourceId.setText(source.getId());
          holder.sourceName.setText(source.getName());
          holder.sourceDescription.setText(source.getDescription());
          holder.sourceURL.setText(source.getUrl());
          holder.sourceCategory.setText(source.getCategory());
          holder.sourceLanguage.setText(source.getLanguage());
          holder.sourceCountry.setText(source.getCountry());
      }
  
      @Override
      public int getItemCount() {
          return sources.size();
      }
      public void setSourceList(List&lt;Source&gt; sources) {
          this.sources = sources;
          notifyDataSetChanged();
      }
  
      public static class SourceSavedViewHolder extends RecyclerView.ViewHolder {
          TextView sourceName, sourceId, sourceDescription, sourceURL, sourceCategory, sourceLanguage, sourceCountry;
  
          public SourceSavedViewHolder(View view) {
              super(view);
  
              sourceName = view.findViewById(R.id.sourceName);
              sourceId = view.findViewById(R.id.sourceIdItem);
              sourceDescription = view.findViewById(R.id.sourceDescription);
              sourceURL = view.findViewById(R.id.sourceURL);
              sourceCategory = view.findViewById(R.id.sourceCategory);
              sourceLanguage = view.findViewById(R.id.sourceLanguage);
              sourceCountry = view.findViewById(R.id.sourceCountry);
          }
      }
  }
  </code></pre>