我有这个查询来更新已经在我的领域表中的数据;使用RxJava异步Android界面更新
for (MyGameEntrySquad squad : response.body().getSquad()) {
subscription = realm.where(RealmPlayer.class).equalTo("id", squad.getPlayer().getId())
.findFirstAsync()
.asObservable()
.subscribe(new Action1<RealmObject>() {
@Override
public void call(RealmObject realmObject) {
}
});
}
我想异步执行此查询,然后在UI上显示结果。
基本上,response.body()。getSquad()有一个id与已经在DB中的记录相匹配;这就是我在equalTo方法中使用的内容。
根据收到的数据,我想更新每个匹配ID的记录的两列。
不过,我对这个面临着一些挑战:
- 的措施1在订阅的,而不是返回RealmObject一个PlayerObject的
- 如何从这里
上的任何指导进行这将不胜感激。
感谢
更新
if (response.isSuccessful()) {
//asynchronously update the existing players records with my squad i.e is_selected
for (MyGameEntrySquad squad : response.body().getSquad()) {
realm.where(RealmPlayer.class).equalTo("id", squad.getPlayer().getId())
.findFirstAsync()
.<RealmPlayer>asObservable()
.filter(realmPlayer -> realmPlayer.isLoaded())
.subscribe(player -> {
realm.beginTransaction();
if (squad.getPlayer().getPosition().equals("GK")) {
player.setPlaygroundPosition("gk");
player.setIsSelected(true);
}
// pick the flex player
if (squad.isFlex()) {
player.setPlaygroundPosition("flex");
player.setIsSelected(true);
}
// pick the Goalie
if (squad.getPlayer().getPosition().equals("GK")) {
player.setPlaygroundPosition("gk");
player.setIsSelected(true);
}
// pick the DFs
if ((squad.getPlayer().getPosition().equals("DF")) && (!squad.isFlex())) {
int dfCounter = 1;
player.setPlaygroundPosition(String.format(Locale.ENGLISH, "df%d", dfCounter));
player.setIsSelected(true);
dfCounter++;
}
// pick the MFs
if ((squad.getPlayer().getPosition().equals("MF")) && (!squad.isFlex())) {
int mfCounter = 1;
player.setPlaygroundPosition(String.format(Locale.ENGLISH, "mf%d", mfCounter));
player.setIsSelected(true);
mfCounter++;
}
// pick the FWs
if ((squad.getPlayer().getPosition().equals("FW")) && (!squad.isFlex())) {
int fwCounter = 1;
player.setPlaygroundPosition(String.format(Locale.ENGLISH, "mf%d", fwCounter));
player.setIsSelected(true);
fwCounter++;
}
realm.copyToRealmOrUpdate(player);
realm.commitTransaction();
updateFieldPlayers();
});
}
hideProgressBar();
}
那么你什么时候取消订阅你的用户?你**做**取消订阅,对吧?你在哪里“更新两列”?你如何显示对象(是一个RecyclerView或其他东西)?这个示例代码运行在哪个线程上?目前没有足够的数据可以正确回答这个问题。 – EpicPandaForce
嗨@EpicPandaForce,我面临的挑战之一就是从那一点开始,这就是为什么有些信息丢失的原因。我在编辑原始问题以显示目前在哪里 –
@EpicPandaForce是我退订;在onDestroy()函数中。我现在使用TableLayout –