title: How to update room database based on server response
body: <p>I am fetching data from server and saving in room database and then from room showing it in recycler view.Data is perfectly saving in room database and showing in recycler view.</p>
  
  <p><strong>Problem:</strong> When I am deleting some data from server database then its old copy that saved earlier still persists in room.</p>
  
  <p><strong>What I want:</strong> I don't want to show data deleted from server in recycler view.So how can I update room database based on server response.</p>
  
  <p>This is what I have done so far:</p>
  
  <p><strong>UserDao.java</strong></p>
  
  <pre><code>@Dao
  public interface UserDao {
  
  @Insert(onConflict = OnConflictStrategy.REPLACE)
  void Insert(User... users);
  
  @Query("SELECT * FROM Users")
  LiveData&lt;List&lt;User&gt;&gt; getRoomUsers();
  }
  </code></pre>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>@Entity(tableName = "Users")
  public class User {
  
  @NonNull
  @PrimaryKey
  private String id;
  
  @ColumnInfo(name = "name")
  @SerializedName("name")
  @Expose
  private String name;
  
  @ColumnInfo(name = "age")
  @SerializedName("age")
  @Expose
  private String age;
  
  public User(String id,String name, String age) {
      this.id = id;
      this.name = name;
      this.age = age;
  }
  
  public String getId() {
      return id;
  }
  
  public void setId(String id) {
      this.id = id;
  }
  
  public String getName() {
      return name;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public String getAge() {
      return age;
  }
  
  public void setAge(String age) {
      this.age = age;
  }
  }
  </code></pre>
  
  <p><strong>UserRepository.java</strong></p>
  
  <pre><code>public class UserRepository {
  
  private Context context;
  private UserDb userDb;
  private LiveData&lt;List&lt;User&gt;&gt; listLiveData;
  
  public UserRepository(Context context) {
      this.context = context;
      userDb = UserDb.getInstance(context);
      listLiveData = userDb.userDao().getRoomUsers();
  }
  
  public void getUserList(){
  
            Retrofit retrofit = RetrofitClient.getInstance();
            ApiService apiService = retrofit.create(ApiService.class);
  
            Call&lt;List&lt;User&gt;&gt; userList = apiService.getUser();
  
            userList.enqueue(new Callback&lt;List&lt;User&gt;&gt;() {
                @Override
                public void onResponse(Call&lt;List&lt;User&gt;&gt; call, final Response&lt;List&lt;User&gt;&gt; response) {
  
                    Completable.fromAction(new Action() {
                            @Override
                            public void run() throws Exception {
  
                                if(response.body() != null) {
  
                                    List&lt;User&gt; list = response.body();
  
                                    for (int i = 0; i &lt; list.size(); i++) {
  
                                        String id = list.get(i).getId();
                                        String names = list.get(i).getName();
                                        String age = list.get(i).getAge();
  
                                        User user = new User(id,names,age);
  
                                        userDb.userDao().Insert(user);
  
  
                                    }
  
                                }
  
                            }
                        }).subscribeOn(Schedulers.io())
                          .observeOn(AndroidSchedulers.mainThread())
                          .subscribe(new CompletableObserver() {
                              @Override
                              public void onSubscribe(Disposable d) {
  
                              }
  
                              @Override
                              public void onComplete() {
  
                                  Toast.makeText(context,"Data inserted",Toast.LENGTH_SHORT).show();
                              }
  
                              @Override
                              public void onError(Throwable e) {
  
                                  Toast.makeText(context,e.getMessage(),Toast.LENGTH_SHORT).show();
                              }
                          });
  
  
                }
  
                @Override
                public void onFailure(Call&lt;List&lt;User&gt;&gt; call, Throwable t) {
                    Toast.makeText(context,t.getMessage(),Toast.LENGTH_LONG).show();
                }
            });
  
  }
  
  public LiveData&lt;List&lt;User&gt;&gt; getRoomUsers(){
  
      return listLiveData;
  }
  }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  UserRepository userRepository;
  RecyclerView recyclerView;
  UserViewModel userModel;
  List&lt;User&gt; userList;
  UserAdapter adapter;
  ProgressBar prg;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Toolbar toolbar = findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      prg = findViewById(R.id.prg);
  
      userRepository = new UserRepository(this);
      userModel = ViewModelProviders.of(this).get(UserViewModel.class);
  
      recyclerView = findViewById(R.id.recyclerView);
      recyclerView.setHasFixedSize(true);
      recyclerView.setLayoutManager(new LinearLayoutManager(this));
  
      userList = new ArrayList&lt;&gt;();
  
      adapter = new UserAdapter(userList,this);
      recyclerView.setAdapter(adapter);
  
      userModel.getListLiveData().observe(this, new Observer&lt;List&lt;User&gt;&gt;() {
  
          @Override
          public void onChanged(List&lt;User&gt; users) {
  
              prg.setVisibility(View.INVISIBLE);
              adapter.setUserList(users);
          }
      });
  
      FloatingActionButton fab = findViewById(R.id.fab);
      fab.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              Intent i = new Intent(MainActivity.this,AddUser.class);
              startActivity(i);
          }
      });
  
  
  
      userRepository.getUserList();
  }
  </code></pre>
  
  <p>Someone please let me know how can I get desired result. Any help would be appreciated.</p>
  
  <p><strong>THANKS</strong> </p>