1

I am working with Room for the first time and notice that there doesn't seem to be any callbacks for when an operation is finished being performed. Does this exist? If not, what is the best practice for inserting or updating data and then immediately retrieving that data. I want a guarentee that the changes were made. I am noticing that unless I have an arbitrary delay before retrieval the data I am getting is old data and not the update data.

Here is an example DAO

@Dao
public interface TestDao {

    @Query("SELECT * FROM test_table")
    List<TestEntity> getAll();

    @Query("DELETE FROM test_table")
    void deleteAll();

    @Insert
    void insert(TestEntity testEntities);

    @Delete
    void delete(TestEntity testEntity);

    @Update
    void update(TestEntity testEntity);
}

Here is my AppDatabase

@Database(entities = {TestEntity.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static final String DATABASE_NAME = "room.test.db";
    private static AppDatabase APP_DATABASE_INSTANCE;

    public abstract TestDao testDao();

    public static AppDatabase getAppdatabase(@NonNull Context context) {
        if (FrameworkUtils.checkIfNull(APP_DATABASE_INSTANCE)) {
            APP_DATABASE_INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                    AppDatabase.class, DATABASE_NAME).build();
        }
        return APP_DATABASE_INSTANCE;
    }

    /**
     * Method is used to destroy database instance
     */
    public static void destroy() {
        APP_DATABASE_INSTANCE = null;
    }
}

Here is how I am performing updates

Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // update database tables
                AppDatabase.getAppdatabase(context).testDao().update(testEntity);

            }
        });

Here is how I am retrieving my data

Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                // get database data
                List<TestEntity> alData = AppDatabase.getAppdatabase(context).testDao().getAll();

            }
        });

I created functions for this code so that I can just do call update(testEntity) and get(). The problem is that I am unable to do back to back operations

TestEntity testEntity = new TestEntity();
testEntity.fname = "firstName"
testEntity.lname = "lastName"

update(testEntity);
get(); // the data fetched is old data unless I wrap this with a 5 second delay or something
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136

1 Answers1

1

You should be using LiveData<List<Entity>> (return it from the Dao) instead of the list itself. This way you can observe the livedata and be alerted everytime the underlying data is updated

Documentation can be found here: https://developer.android.com/topic/libraries/architecture/livedata

Nick Mowen
  • 2,572
  • 2
  • 22
  • 38
  • Do you have an example of what you are talking about? I will accept your answer, I understand what you mean. For others, a good example is here: https://stackoverflow.com/questions/47215666/room-livedata-from-dao-will-trigger-observer-onchanged-on-every-update-even-i – portfoliobuilder Nov 29 '18 at 18:44
  • is there another way instead of LiveData? – ysfcyln Apr 17 '20 at 17:58