[英]How to use observable in onComplete (Rxjava2)
I'm writing my application with as much as I can of rxjava and I'm having the following problem. 我正在用rxjava编写尽可能多的应用程序,但遇到了以下问题。
I Have a method that syncs data with the api, and I download stuff based on a timestamp. 我有一种与api同步数据的方法,并且我根据时间戳下载内容。 So I would like the following data flow:
所以我想要以下数据流:
Get last timestamp -> Download data -> Save data -> Update timestamp if no errors ocurred 获取上一个时间戳->下载数据->保存数据->更新时间戳(如果未发生错误)
The problem is that I receive the timestamp from the api response to avoid date differences between devices, and as soon as I transform the response from the api I loose the timestamp. 问题是我从api响应中收到时间戳,以避免设备之间的日期差异,并且一旦我从api转换响应,我就会松开时间戳。
Here is a simplification of the current flow: 这是电流的简化:
class Response
{
Date timeStamp;
Data data;
}
interface IRepository
{
Completable insert(Data data);
}
interface IWebService
{
Observable<Response> getByLastUpdate(Date date);
}
class SyncPreferences
{
public Date getLastDownloadDate() { /**/ }
public void setLastDownloadDate(Date date){ /**/ }
}
public class SyncService
{
private final IRepository repository;
private final IWebService webService;
private final SyncPreferences syncPreferences;
public SyncService(IRepository repository, SyncPreferences syncPreferences, IWebService webService)
{
this.repository = repository;
this.webService = webService;
this.syncPreferences = syncPreferences;
}
private Completable sync()
{
return webService.getByLastUpdate(syncPreferences.getLastDownloadDate())
.doOnComplete((response) -> {
syncPreferences.setLastDownloadDate(response.timeStamp)
}) // What I would like to do
.flatMapCompletable((response) -> {
repository.insert(response.data);
});
}
}
The problem is that doOnComplete
method does not receive the parameter ( response
) from the observable and I would like to set the timestamp as the last thing so if an error occurs I can just retry downloading with the old timestamp. 问题在于
doOnComplete
方法没有从可观察对象接收参数( response
),我想将时间戳设置为最后一件事,因此,如果发生错误,我可以重试使用旧时间戳进行下载。
The only solution I found was make a transaction mechanism in the SyncPreferences
that I could use like this: 我发现的唯一解决方案是在
SyncPreferences
中建立事务处理机制,如下所示:
private Completable sync()
{
return webService.getByLastUpdate(syncPreferences.getLastDownloadDate())
.doOnNext((response) -> {
syncPreferences.setLastDownloadDate(response.timeStamp);
})
.flatMapCompletable((response) -> {
repository.insert(response.data);
})
.andThen(syncPreferences.commitChanges());
}
I am new to RxJava so if you have suggestions in general I would be glad to know =] 我是RxJava的新手,所以如果您有一般建议,我将很高兴知道=]
PS I don't know if this code compiles since I just wrote it here for the example. PS我不知道该代码是否可以编译,因为我只是在此处为示例编写代码。
What about this: 那这个呢:
webService.getByLastUpdate(syncPreferences.getLastDownloadDate())
.flatMapCompletable(response ->
repository
.insert(response.data)
.doOnComplete(() ->
syncPreferences.setLastDownloadDate(response.timeStamp)
)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.