gpt4 book ai didi

java - RxJava。通过少数来源生成 ViewObject 的正确方法

转载 作者:行者123 更新时间:2023-12-02 02:55:56 24 4
gpt4 key购买 nike

我有两个实体类,存储在SQLite中:UserEntitySessionInfoEntity:

public class UserEntity{
private Long id;
private String userName;
// ....
}

public class SessionInfoEntity{
private Long id;

private Date beginSessionDate;
private Date endSessionDate;
// ....
}

用户可以有多个 session (一对多关系)。

我有一个存储库,它提供了从 SQLite 数据库获取数据(RxJava observables)所需的方法:

public class MyRepository{
public Observable<List<UserEntity>> getAllUsers(){/* ... */}
public Observable<SessionInfoEntity> getLastSessionInfoForUser(Long userId){/* ... */} // returns info of last session for user with id=userId
}

我需要使用 MyRepository 的方法和 RxJava 为每个用户生成下一个 ViewObject:

public class UserViewObject {
private String userName;
private Integer lastSessionDurationInHours;
// ....
}

事实证明,我需要为每个用户调用 getLastSessionInfoForUser() 才能创建 UserViewObject

问题:如何正确使用RxJava生成UserViewObject

我正在尝试以这种方式开始这样做:

myRepository
.getAllUsers()
.flatMap(lst -> Observable.from(lst))
.flatMap(ve -> getLastSessionInfoForUser(ve.getId())
.map(lse -> /* ????? */) // In this operator I lose access to current user => I can't generate UserViewObject, because I haven't access to ve.getUserName() method

P.S.:我无法在 MyRepository 中编写方法,该方法将返回包含完整信息的对象。

P.P.S.:将来,将添加与 User 实体相关的新方法(例如 getLastSessionInfoForUser() 方法)。

最佳答案

您可以在最后一个flatMap中添加 map 。就像你可以访问ve

myRepository
.getAllUsers()
.flatMap(lst -> Observable.from(lst))
.flatMap(ve -> getLastSessionInfoForUser(ve.getId()).map(lse -> /* ... */))

关于java - RxJava。通过少数来源生成 ViewObject 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43069194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com