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

-7 android android-room

我正在使用roomandroid.当我在数据库中插入内容时,我获取当前正在运行的应用程序的数据,但是当我重新启动应用程序时,屏幕上没有任何内容.如何获取以前存储的数据without using LiveData

mag*_*man 6

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

如何在不使用LiveData的情况下获取以前存储的数据?如何room使用库中存储的所有行Async Task

说明:

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

  @Query("SELECT * FROM user")
    List<CustomObject> getAll();
Run Code Online (Sandbox Code Playgroud)

在您的Repository类中包含以下代码段:

 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();
    }
}
Run Code Online (Sandbox Code Playgroud)