I have two entity classes, which stores in SQLite : UserEntity
and SessionInfoEntity
:
public class UserEntity{
private Long id;
private String userName;
// ....
}
public class SessionInfoEntity{
private Long id;
private Date beginSessionDate;
private Date endSessionDate;
// ....
}
User can have many sessions (one-to-many relation).
I have a repository, which provides necessary methods to get data (RxJava observables) from my SQLite database:
public class MyRepository{
public Observable<List<UserEntity>> getAllUsers(){/* ... */}
public Observable<SessionInfoEntity> getLastSessionInfoForUser(Long userId){/* ... */} // returns info of last session for user with id=userId
}
I need to generate next ViewObject for each User, using MyRepository
's methods and RxJava :
public class UserViewObject {
private String userName;
private Integer lastSessionDurationInHours;
// ....
}
In turns out that I need to call getLastSessionInfoForUser()
for each User in order to create UserViewObject
.
QUESTION: How I can generate UserViewObject
using RxJava correctly?
I'm trying to start doing this this way:
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
PS: I can't write method in MyRepository, which will be returns object with whole information.
PPS: In the future, new methods will be added that are related to the User entity (like getLastSessionInfoForUser()
method).
You can add the map in the last flatMap
. Like that you have access to ve
.
myRepository
.getAllUsers()
.flatMap(lst -> Observable.from(lst))
.flatMap(ve -> getLastSessionInfoForUser(ve.getId()).map(lse -> /* ... */))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.