我可以获取项目列表并基于 id 获取唯一列表并将两个列表都放在一个 Flowable 中吗?
Can I fetch the list of items and based on id get unique list and have in one Flowable both lists?
我从 ShoppingList 项目的数据库列表中获取
每个项目都有 recipeId,例如,如果我有 10 个购物清单项目
其中 8 个可以具有相同的 recipeId,其余的购物列表项目另一个 recipeId.It 意味着 10 个项目可以代表 2 个食谱。
所以我获取了 10 个购物清单项目,然后我有 10 个 recipeIds。我应该保留唯一的 recipeIds(在这种情况下我们有 2 个 recipeIds),然后获取该 recipeIds 的食谱,注意:我需要显示 10 个购物清单项目和 2 个食谱。
我可以在 Flowable 中获取所有这些信息吗,
或者我需要订阅 shoppingListItems,然后在基于此列表的单独流动中有唯一 recipeIds 列表
如果可能的话,哪个是最好的解决方案
比方说,您有购物商品并且想要获取每个唯一的 recepId 信息:
环境::
RxJava 2.0.7
assertJ 3.8.0
--
此代码将 recepId(唯一)分组并请求每个 recepId 的 recep,并将结果连接回 ShoppingItem。
@Test
void name() {
Observable<Tuple2<ShoppingListItem, Recep>> tuple = getFromDb().flatMapIterable(i -> i)
.groupBy(shoppingListItem -> shoppingListItem.recepId)
.flatMap(g -> {
Observable<Recep> recep = getRecep(g.getKey());
return Observable.combineLatest(recep, g, (recep1, shoppingListItem) -> {
return Tuple.of(shoppingListItem, recep1);
});
});
TestObserver<Tuple2<ShoppingListItem, Recep>> test = tuple.test()
.assertValueCount(4)
.assertComplete()
.assertNoErrors();
List<Tuple2<ShoppingListItem, Recep>> values = test.values();
assertThat(values).extracting("_1").extracting("recepId").contains(0, 1);
assertThat(values).extracting("_1").extracting("id").containsExactly(0, 1, 2, 3);
assertThat(values).extracting("_2").extracting("id").containsExactly(1, 1, 0, 0);
}
Observable<List<ShoppingListItem>> getFromDb() {
List<ShoppingListItem> shoppingListItems = Arrays.asList(
new ShoppingListItem(0, 1),
new ShoppingListItem(1, 1),
new ShoppingListItem(2, 0),
new ShoppingListItem(3, 0)
);
return Observable.just(shoppingListItems);
}
Observable<Recep> getRecep(int id) {
return Observable.just(new Recep(id));
}
class Recep {
private final int id;
public Recep(int id) {
this.id = id;
}
}
class ShoppingListItem {
private final int id;
private final int recepId;
public ShoppingListItem(int id, int recepId) {
this.id = id;
this.recepId = recepId;
}
}
Return 分组对象中的 ShoppingItems 列表和 Receps 列表:
@Test
void name() {
Observable<Items> itemsObservable = getFromDb().flatMapSingle(shoppingListItems -> {
List<Integer> collect = shoppingListItems.stream().map(i -> i.recepId)
.distinct()
.collect(Collectors.toList());
return Observable.fromIterable(collect).flatMap(this::getRecep)
.toList()
.zipWith(Single.just(collect), (receps, integers) -> {
return new Items(receps, shoppingListItems);
});
});
TestObserver<Items> test = itemsObservable.test()
.assertValueCount(1)
.assertComplete()
.assertNoErrors();
Items items = test.values().get(0);
assertThat(items.receps).hasSize(2);
assertThat(items.shoppingListItems).hasSize(4);
}
Observable<List<ShoppingListItem>> getFromDb() {
List<ShoppingListItem> shoppingListItems = Arrays.asList(
new ShoppingListItem(0, 1),
new ShoppingListItem(1, 1),
new ShoppingListItem(2, 0),
new ShoppingListItem(3, 0)
);
return Observable.just(shoppingListItems);
}
Observable<Recep> getRecep(int id) {
return Observable.just(new Recep(id));
}
class Items {
private final List<Recep> receps;
private final List<ShoppingListItem> shoppingListItems;
Items(List<Recep> receps, List<ShoppingListItem> shoppingListItems) {
this.receps = receps;
this.shoppingListItems = shoppingListItems;
}
}
class Recep {
private final int id;
public Recep(int id) {
this.id = id;
}
}
class ShoppingListItem {
private final int id;
private final int recepId;
public ShoppingListItem(int id, int recepId) {
this.id = id;
this.recepId = recepId;
}
}
我从 ShoppingList 项目的数据库列表中获取 每个项目都有 recipeId,例如,如果我有 10 个购物清单项目 其中 8 个可以具有相同的 recipeId,其余的购物列表项目另一个 recipeId.It 意味着 10 个项目可以代表 2 个食谱。 所以我获取了 10 个购物清单项目,然后我有 10 个 recipeIds。我应该保留唯一的 recipeIds(在这种情况下我们有 2 个 recipeIds),然后获取该 recipeIds 的食谱,注意:我需要显示 10 个购物清单项目和 2 个食谱。
我可以在 Flowable 中获取所有这些信息吗, 或者我需要订阅 shoppingListItems,然后在基于此列表的单独流动中有唯一 recipeIds 列表
如果可能的话,哪个是最好的解决方案
比方说,您有购物商品并且想要获取每个唯一的 recepId 信息:
环境::
RxJava 2.0.7
assertJ 3.8.0
--
此代码将 recepId(唯一)分组并请求每个 recepId 的 recep,并将结果连接回 ShoppingItem。
@Test
void name() {
Observable<Tuple2<ShoppingListItem, Recep>> tuple = getFromDb().flatMapIterable(i -> i)
.groupBy(shoppingListItem -> shoppingListItem.recepId)
.flatMap(g -> {
Observable<Recep> recep = getRecep(g.getKey());
return Observable.combineLatest(recep, g, (recep1, shoppingListItem) -> {
return Tuple.of(shoppingListItem, recep1);
});
});
TestObserver<Tuple2<ShoppingListItem, Recep>> test = tuple.test()
.assertValueCount(4)
.assertComplete()
.assertNoErrors();
List<Tuple2<ShoppingListItem, Recep>> values = test.values();
assertThat(values).extracting("_1").extracting("recepId").contains(0, 1);
assertThat(values).extracting("_1").extracting("id").containsExactly(0, 1, 2, 3);
assertThat(values).extracting("_2").extracting("id").containsExactly(1, 1, 0, 0);
}
Observable<List<ShoppingListItem>> getFromDb() {
List<ShoppingListItem> shoppingListItems = Arrays.asList(
new ShoppingListItem(0, 1),
new ShoppingListItem(1, 1),
new ShoppingListItem(2, 0),
new ShoppingListItem(3, 0)
);
return Observable.just(shoppingListItems);
}
Observable<Recep> getRecep(int id) {
return Observable.just(new Recep(id));
}
class Recep {
private final int id;
public Recep(int id) {
this.id = id;
}
}
class ShoppingListItem {
private final int id;
private final int recepId;
public ShoppingListItem(int id, int recepId) {
this.id = id;
this.recepId = recepId;
}
}
Return 分组对象中的 ShoppingItems 列表和 Receps 列表:
@Test
void name() {
Observable<Items> itemsObservable = getFromDb().flatMapSingle(shoppingListItems -> {
List<Integer> collect = shoppingListItems.stream().map(i -> i.recepId)
.distinct()
.collect(Collectors.toList());
return Observable.fromIterable(collect).flatMap(this::getRecep)
.toList()
.zipWith(Single.just(collect), (receps, integers) -> {
return new Items(receps, shoppingListItems);
});
});
TestObserver<Items> test = itemsObservable.test()
.assertValueCount(1)
.assertComplete()
.assertNoErrors();
Items items = test.values().get(0);
assertThat(items.receps).hasSize(2);
assertThat(items.shoppingListItems).hasSize(4);
}
Observable<List<ShoppingListItem>> getFromDb() {
List<ShoppingListItem> shoppingListItems = Arrays.asList(
new ShoppingListItem(0, 1),
new ShoppingListItem(1, 1),
new ShoppingListItem(2, 0),
new ShoppingListItem(3, 0)
);
return Observable.just(shoppingListItems);
}
Observable<Recep> getRecep(int id) {
return Observable.just(new Recep(id));
}
class Items {
private final List<Recep> receps;
private final List<ShoppingListItem> shoppingListItems;
Items(List<Recep> receps, List<ShoppingListItem> shoppingListItems) {
this.receps = receps;
this.shoppingListItems = shoppingListItems;
}
}
class Recep {
private final int id;
public Recep(int id) {
this.id = id;
}
}
class ShoppingListItem {
private final int id;
private final int recepId;
public ShoppingListItem(int id, int recepId) {
this.id = id;
this.recepId = recepId;
}
}