Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room Update not Updating Database

I'm new to Room and am having some trouble updating the database. Insert and delete work as expected but the update does not. The data remains the same in the database. I've tried the update using the entire object and the specific field in an @Query statement. What am I doing wrong?

Entity:

@Entity(tableName = "Appointments")
public class Appointment {

@PrimaryKey
@NonNull
public int AppointmentID;
public Boolean CheckInStatus;

public Appointment(
        @NonNull int AppointmentID,
        Boolean CheckInStatus
) {

    this.AppointmentID = AppointmentID;
    this.CheckInStatus = CheckInStatus;

}

public void setCheckInStatus(Boolean checkInStatus) {
    CheckInStatus = checkInStatus;
}

@NonNull
public int getAppointmentID() {
    return this.AppointmentID;
}

public Boolean getCheckInStatus() {
    return CheckInStatus;
}

public void setID(@NonNull int AppointmentID) {
    this.AppointmentID = AppointmentID;
}

Dao:

@Update
void update(Appointment appointment);

@Query("UPDATE Appointments SET CheckInStatus = 1 WHERE AppointmentID = :Id")
void updateCheckIn(int Id);

Repo:

public void updateCheckIn(int Id) {
    new AppointmentRepository.updateCheckInAsyncTask(mAppointmentDao, Id);
}

public static class updateCheckInAsyncTask extends AsyncTask<Void, Void, Void> {

    private AppointmentDao mAsyncTaskDao;
    private int mId;

    updateCheckInAsyncTask(AppointmentDao dao, int Id) {

        mAsyncTaskDao = dao;
        mId = Id;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        mAsyncTaskDao.updateCheckIn(mId);
        return null;
    }
}

public void update(Appointment appointment) {
    new AppointmentRepository.updateAsyncTask(mAppointmentDao, appointment);
}

private static class updateAsyncTask extends AsyncTask<Void, Void, Void> {

    private AppointmentDao mAsyncTaskDao;
    private Appointment mAppointment;

    updateAsyncTask(AppointmentDao dao, Appointment appointment) {

        mAsyncTaskDao = dao;
        mAppointment = appointment;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        mAsyncTaskDao.update(mAppointment);
        return null;
    }
}

View Model:

public void update(Appointment appointment) { mRepository.update(appointment); }

public void updateCheckIn(int Id) { mRepository.updateCheckIn(Id); }

Activity:

// Update check in status in table
mAppointmentVM.updateCheckIn(mId);

The correct id is being passed as was the updated object when I went that route. Any assistance is greatly appreciated.

EDIT: The final answer is:

   public void updateCheckIn(int Id) {
    new AppointmentRepository.updateCheckInAsyncTask(mAppointmentDao, Id).execute();
}

public static class updateCheckInAsyncTask extends AsyncTask<Void, Void, Void> {

    private AppointmentDao mAsyncTaskDao;
    private int mId;

    updateCheckInAsyncTask(AppointmentDao dao, int Id) {

        mAsyncTaskDao = dao;
        mId = Id;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        Appointment entity = mAsyncTaskDao.getAppointmentDetails(mId);
        entity.setCheckInStatus(true);
        mAsyncTaskDao.update(entity);
        return null;
    }
}
like image 238
Energy CJ Avatar asked Nov 02 '25 15:11

Energy CJ


1 Answers

You could simply do it like that:

  1. Get item that you want to update from DB;
  2. Update that POJO by setting CheckInStatus with setter method;
  3. Simply use @Update(onConflict = OnConflictStrategy.REPLACE) function and pass your updated POJO to it (because Id's match and its a Primary key, DB will automatically identify this situation and update all required fields for required object);

**Example:**
@Override
protected Void doInBackground(Void... voids) {
    Appointment entity = mAsyncTaskDao.getAppointmentById(mId);
    entity.setCheckInStatus(true);
    mAsyncTaskDao.update(entity);
    return null;
}

That is it :) Good luck.

like image 85
Tomas Jablonskis Avatar answered Nov 04 '25 08:11

Tomas Jablonskis