简体   繁体   中英

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;
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM