Java 如何从订阅服务器返回可观察的?
我第一次在我的应用程序中使用rxJava,我试图实现以下实现:Java 如何从订阅服务器返回可观察的?,java,java-8,stream,rx-java,observable,Java,Java 8,Stream,Rx Java,Observable,我第一次在我的应用程序中使用rxJava,我试图实现以下实现: 从第三方服务器获取帐户 从本地数据库获取帐户 比较帐户并筛选出不在本地数据库中的帐户 仅将不在本地数据库中的帐户保存在本地数据库中 这是我的密码:- private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts){
private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts){
accountDAL.getByIds(context, accounts
.stream()
.map(a -> Long.valueOf(a.getAccountId()))
.collect(Collectors.toList()))//return Observable<List<T>> getByIds(Context context, List<Long> ids)
.map( a -> {
Map<Long, SearchConnectAccount> map = a.stream()
.collect(Collectors.toMap(a -> a.getId(), Function.identity())); // map ==> {id = Account}
return map;
}).subscribe( seMap -> { // subscribe observable
List<Account> filteredList = accounts.stream()
.filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null)
.collect(Collectors.toList());
Observable<List<Result<Account, IError>>> o = accountDAL.save(context, filteredList).first();
return o;//accountDAL.save(context, filteredList).first();
});
// how to return Observable<List<Result<Account, IError>>> from here
}
私有可观察筛选器帐户(上下文、列表帐户){
accountDAL.getById(上下文、帐户
.stream()
.map(a->Long.valueOf(a.getAccountId()))
.collect(Collectors.toList())//返回可观察的getById(上下文,列表ID)
.地图(a->{
Map Map=a.stream()
.collect(Collectors.toMap(a->a.getId(),Function.identity());//map=>{id=Account}
返回图;
}).subscribe(seMap->{//subscribe可观察
List filteredList=accounts.stream()
.filter(a->seMap.get(Long.valueOf(a.getAccountId())==null)
.collect(Collectors.toList());
Observable o=accountDAL.save(上下文,filteredList.first();
return o;//accountDAL.save(context,filteredList.first();
});
//如何从这里返回可观测数据
}
非常感谢您的帮助。您可以这样做
private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts){
return accountDAL.getByIds(context, accounts
.stream()
.map(a -> Long.valueOf(a.getAccountId()))
.collect(Collectors.toList()))
.map(a ->
a.stream()
.collect(Collectors.toMap(a -> a.getId(), Function.identity())) // map ==> {id = Account}
).map(seMap ->
accountDAL.save(context, accounts.stream()
.filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null)
.collect(Collectors.toList())).first());
}
它显示编译时错误=>缺少返回语句在哪一行得到这个?在最后一个块->不存在类型变量的实例,因此Observable符合列表推理变量R具有不兼容的边界:等式约束:列表下界:ObservableSorry,但我在最后一块地图上也遇到了同样的问题,更新后的问题你们现在能看到吗
private Observable<List<Result<Account, IError>>> filterAccounts(Context context, List<Account> accounts) {
return accountDAL
.getByIds(context,
accounts.stream().map(a -> Long.valueOf(a.getAccountId())).collect(Collectors.toList()))
.map(ar -> ar.stream().collect(Collectors.toMap(Account::getAccountId, Function.identity())) // map ==>
// {id =
// Account}
).flatMap(seMap -> accountDAL.save(context, accounts.stream()
.filter(a -> seMap.get(Long.valueOf(a.getAccountId())) == null).collect(Collectors.toList())));
}