如何使用 `Async Task` 获取存储在 `room` 库中的所有行?

How to get all rows stored in the `room` library using `Async Task`?

我将 room 库与 android 一起使用。当我在数据库中插入一些东西时,我得到了当前 运行 应用程序的数据,但是当我重新启动应用程序时,屏幕上什么也没有。如何获取之前存储的数据 without using LiveData ?

如果您期待获得以前的数据,以下Codelab 可能会对您有所帮助。但这实际上取决于您的实施。

How to get previous stored data without using LiveData ? How to get all rows stored in the room library using Async Task?

解释:

在你的道中包含以下代码:

  @Query("SELECT * FROM user")
    List<CustomObject> getAll();

在您的存储库 class 中包含此片段:

 public List<CustomObject> getAll() throws ExecutionException, InterruptedException {
    return new getAllAsyncTask(mCustomObjectDao).execute().get();
}

private static class getAllAsyncTask extends android.os.AsyncTask<Void, Void, List<CustomObject>> {

    private CustomObjectDao mAsyncTaskDao;
    List<CustomObject> a;

    getAllAsyncTask(CustomObjectDao dao) {
        mAsyncTaskDao = dao;
    }

    @Override
    protected List<CustomObject> doInBackground(Void... voids) {
        return mAsyncTaskDao.getAll();
    }
}