【发布时间】:2016-09-13 03:39:09
【问题描述】:
我有这个查询来更新我的领域表中已经存在的数据;
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;这就是我在 equalTo 方法中使用的方法。
根据收到的数据,我想更新与 ID 匹配的每条记录上的两列。
但是,我在这方面面临一些挑战:
- subscribe 中的 Action1 返回的是 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 是的,我取消订阅;在 onDestroy() 函数中。我现在正在使用 TableLayout
-
为什么不用
RecyclerView和GridLayoutManager -
@EpicPandaForce 我的 UseCase 不需要 RecyclerView
标签: android asynchronous realm rx-java