RoomDatabase.clearAllTables() 方法执行完毕如何知道?

huangapple go评论72阅读模式
英文:

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

问题

RoomDatabase#clearAllTables() 返回 Unit。那么,如何知道它何时完成?是否可能?

更新:这是从 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");
        }
    }
}
英文:

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

答案1

得分: 1

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.

英文:

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.

huangapple
  • 本文由 发表于 2023年5月21日 00:52:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定