如何在从新的房间持久性库加载数据时显示加载程序并进行失败或成功等回调
How to show loader while loading data from new room persistence library and have callbacks like failure or success
我正在尝试为我的本地数据库使用空间,我的问题是我的应用程序的第一个屏幕显示保存在房间库中的设备列表,但房间不提供任何回调,如成功或失败,所以你如何伙计们处理那个?,还有我如何在从房间加载数据时显示加载程序,因为我无法从线程处理 ui 直到它在 ui 线程上运行,这是房间不推荐的.
private void getDevices(){
AsyncTask.execute(new Runnable() {
@Override
public void run() {
devicesList =
AppDatabase.getAppDatabase(getActivity()).deviceDao().getAllDevices();
devicesAdapter.notifyDataSetChanged();
}
});
}
Room Database does support Asynchronous queries —queries that return
instances of LiveData or Flowable—are exempt from this rule because
they asynchronously run the query on a background thread when needed.
例如,除了 return 原始数据之外,您还可以使用如下内容:
@Dao
public interface MyDao {
@Query("SELECT * from user where id = :id LIMIT 1")
public LiveData<User> loadUserById(int id);
}
或
@Dao
public interface MyDao {
@Query("SELECT * from user where id = :id LIMIT 1")
public Flowable<User> loadUserById(int id);
}
通过使用 Flowable
,您可以像这样收听对 show/hide 进度对话框的回调:
AppDatabase.getAppDatabase(getActivity()).deviceDao().getAllDevices()
.observeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe((Consumer<Subscription>) subscription -> {
// do something when subscribe
// show progress here
})
.doOnTerminate((Action) () -> {
// do something when success or fail
// hide progress here
})
.subscribe(device -> {
// load data success
}, throwable -> {
// do when error
});
使用 RxJava 的响应式查询
Room 为 RxJava2 类型的 return 值提供以下支持:
@Query methods: Room supports return values of type Publisher,
Flowable, and Observable. @Insert, @Update, and @Delete methods: Room
2.1.0 and higher supports return values of type Completable, Single, and Maybe.
要使用此功能,请在您应用的 build.gradle
文件中包含最新版本的 rxjava2 工件:
app/build.gradle
dependencies {
implementation 'androidx.room:room-rxjava2:2.1.0-alpha02'
}
我正在尝试为我的本地数据库使用空间,我的问题是我的应用程序的第一个屏幕显示保存在房间库中的设备列表,但房间不提供任何回调,如成功或失败,所以你如何伙计们处理那个?,还有我如何在从房间加载数据时显示加载程序,因为我无法从线程处理 ui 直到它在 ui 线程上运行,这是房间不推荐的.
private void getDevices(){
AsyncTask.execute(new Runnable() {
@Override
public void run() {
devicesList =
AppDatabase.getAppDatabase(getActivity()).deviceDao().getAllDevices();
devicesAdapter.notifyDataSetChanged();
}
});
}
Room Database does support Asynchronous queries —queries that return instances of LiveData or Flowable—are exempt from this rule because they asynchronously run the query on a background thread when needed.
例如,除了 return 原始数据之外,您还可以使用如下内容:
@Dao
public interface MyDao {
@Query("SELECT * from user where id = :id LIMIT 1")
public LiveData<User> loadUserById(int id);
}
或
@Dao
public interface MyDao {
@Query("SELECT * from user where id = :id LIMIT 1")
public Flowable<User> loadUserById(int id);
}
通过使用 Flowable
,您可以像这样收听对 show/hide 进度对话框的回调:
AppDatabase.getAppDatabase(getActivity()).deviceDao().getAllDevices()
.observeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe((Consumer<Subscription>) subscription -> {
// do something when subscribe
// show progress here
})
.doOnTerminate((Action) () -> {
// do something when success or fail
// hide progress here
})
.subscribe(device -> {
// load data success
}, throwable -> {
// do when error
});
使用 RxJava 的响应式查询
Room 为 RxJava2 类型的 return 值提供以下支持:
@Query methods: Room supports return values of type Publisher, Flowable, and Observable. @Insert, @Update, and @Delete methods: Room 2.1.0 and higher supports return values of type Completable, Single, and Maybe.
要使用此功能,请在您应用的 build.gradle
文件中包含最新版本的 rxjava2 工件:
app/build.gradle
dependencies {
implementation 'androidx.room:room-rxjava2:2.1.0-alpha02'
}