我是rxAndroid的新手,我只是简单地用rxAndroid替换asynTask。我只想在后台使用db query创建一个列表
new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
startSppiner();
}
@Override
protected Void doInBackground(Void... params) {
List<masterDataTable> list = ServatiumApplication.daoSession.getMasterDataTableDao().queryBuilder().where(masterDataTableDao.Properties.MasterType.eq(ParserString.MD_SERVICE_LEVEL), masterDataTableDao.Properties.DeleteFlag.eq(SHOW_FLAG)).orderAsc(masterDataTableDao.Properties.Description).list();
if (list.size() > 0) {
PopUpValues values1 = new PopUpValues();
values1.setValue(ParserString.SELECT_VALUE);
values1.setName(ParserString.SELECT);
serviceLevelList.add(values1);
for (masterDataTable item : list) {
PopUpValues values = new PopUpValues();
values.setValue(item.getLookupCode());
values.setName(item.getDescription());
serviceLevelList.add(values);
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
stopSppiner();
}
}.execute();
提前感谢您的帮助!!
答案 0 :(得分:0)
这样做。
public void query(){
startSppiner();
Observable.fromCallable(new Func0<List<PopUpValues>>() {
@Override
public List<PopUpValues> call() {
List<masterDataTable> list = ServatiumApplication.daoSession.getMasterDataTableDao().queryBuilder().where(masterDataTableDao.Properties.MasterType.eq(ParserString.MD_SERVICE_LEVEL), masterDataTableDao.Properties.DeleteFlag.eq(SHOW_FLAG)).orderAsc(masterDataTableDao.Properties.Description).list();
List<PopUpValues> serviceLevelList = new ArrayList<PopUpValues>();
if (list.size() > 0) {
PopUpValues values1 = new PopUpValues();
values1.setValue(ParserString.SELECT_VALUE);
values1.setName(ParserString.SELECT);
serviceLevelList.add(values1);
for (masterDataTable item : list) {
PopUpValues values = new PopUpValues();
values.setValue(item.getLookupCode());
values.setName(item.getDescription());
serviceLevelList.add(values);
}
}
return serviceLevelList;
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<List<PopUpValues>>() {
@Override
public void onCompleted() {
stopSppiner();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(List<PopUpValues> popUpValues) {
// Here you get the serviceLevelList data
}
});
}
答案 1 :(得分:0)
下面是我尝试使用上面提供的AsyncTask代码转换为Rx的代码。您可能需要进行一些修改,但是总体流程保持不变。
Observable
.fromIterable(
ServatiumApplication.daoSession
.getMasterDataTableDao()
.queryBuilder()
.where(
masterDataTableDao
.Properties
.MasterType
.eq(ParserString.MD_SERVICE_LEVEL),
masterDataTableDao.Properties.DeleteFlag.eq(SHOW_FLAG)
)
.orderAsc(masterDataTableDao.Properties.Description)
.list()
)
//Get item one by one from the above list, create a PopupValues object and add to it
.map(
listItem -> {
PopUpValues values = new PopUpValues();
values.setValue(listItem.getLookupCode());
values.setName(listItem.getDescription());
return values;
}
)
//Convert to List<PopupValues> list. toList() returns Single<List<PopupValues>>
.toList()
//When subscribe is called show spinner
.doOnSubscribe(disposable -> startSppiner())
//When job is done successfully or fails stop spinner
.doOnSuccess(listOfPopupValues -> stopSppiner())
.doOnError(throwable -> stopSpinner())
//Do the job in io() thread
.subscribeOn(Schedulers.io())
//Give the resulting list to mainthread
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
result -> "get the List<PopUpValues>",
error -> new Throwable("")
);