Search code examples
androidandroid-room

How can I know if RoomDatabase.clearAllTables() has finished?


RoomDatabase#clearAllTables() returns Unit. So, how can one know when it has finished? Is that possible at all?

Update: That is the generated code taken from AppDatabase_Impl.java

  @Override
  public void clearAllTables() {
    super.assertNotMainThread();
    final SupportSQLiteDatabase _db = super.getOpenHelper().getWritableDatabase();
    boolean _supportsDeferForeignKeys = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP;
    try {
      if (!_supportsDeferForeignKeys) {
        _db.execSQL("PRAGMA foreign_keys = FALSE");
      }
      super.beginTransaction();
      if (_supportsDeferForeignKeys) {
        _db.execSQL("PRAGMA defer_foreign_keys = TRUE");
      }
      _db.execSQL("DELETE FROM `something`");
      _db.execSQL("DELETE FROM `something2`");
      super.setTransactionSuccessful();
    } finally {
      super.endTransaction();
      if (!_supportsDeferForeignKeys) {
        _db.execSQL("PRAGMA foreign_keys = TRUE");
      }
      _db.query("PRAGMA wal_checkpoint(FULL)").close();
      if (!_db.inTransaction()) {
        _db.execSQL("VACUUM");
      }
    }
  }

Solution

  • The generated code for clearAllTables() is synchronous. You can see from the generated code in your question that the function implements the database transaction and deletes the contents of the tables directly within that function. All of the functions called on _db, such as execSQL(), are synchronous.