从可处理到可观察



我会尽量说得更清楚。

我想循环列表中的元素,对于MAIN_LIST中的每个元素,开始详细说明。阐述包括另一个列表,SECOND_LIST,必须循环。当SECOND_LIST的每个项目的所有阐述完成后,开始对MAIN_LIST中的下一个元素执行相同的操作。

完成MAIN_LIST中的所有元素的详细说明后,返回完成。

这是我试图实现这一目标,但我认为有更好的方法。

感谢您的帮助!

循环MAIN_LIST的方法

public Completable checkGroupExpiration(List<CheckVersion.ServiceStatus> serviceStatusList) {
return Completable.create(emitter -> {
Observable.fromIterable(serviceStatusList)
.concatMapCompletable(serviceStatus -> {
return checkGroupExpiration(serviceStatus.service, serviceStatus.lastUpdate);
}).subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
if (!emitter.isDisposed())
emitter.onComplete();
}
@Override
public void onError(Throwable e) {
if (!emitter.isDisposed())
emitter.onComplete();
}
});
});
}

循环SECOND_LIST的方法

protected Completable checkGroupExpiration(String group, long ttl) {
return Completable.create(emitter -> {
readFile(MASTER_NOTE)
.map(s -> {
return new Gson().fromJson(s, MasterNote.class);
}).flatMapObservable(masterNote -> {
return Observable.fromIterable(masterNote.savedFiles.entrySet());
}).filter(stringCacheInfoEntry -> {
return stringCacheInfoEntry.getValue().group.equals(group) && stringCacheInfoEntry.getValue().ttl < ttl;
}).concatMapCompletable(stringCacheInfoEntry -> {
return getFile(stringCacheInfoEntry.getKey(), false)
.doOnSuccess(file -> {
String fileName = file.getName();
file.delete();
Log.d(TAG, "File deleted => " + fileName + " from group => " + group);
}).ignoreElement();
}).subscribe(new CompletableObserver() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onComplete() {
if (!emitter.isDisposed())
emitter.onComplete();
}
@Override
public void onError(Throwable e) {
if (!emitter.isDisposed())
emitter.onComplete();
}
});
});
}

是的,有。不要订阅create内部流,而是直接使用这些流:

public Completable checkGroupExpiration(
List<CheckVersion.ServiceStatus> serviceStatusList) {
retrn Observable.fromIterable(serviceStatusList)
.concatMapCompletable(serviceStatus -> 
checkGroupExpiration(serviceStatus.service, serviceStatus.lastUpdate)
)
.ignoreElements();
}
protected Completable checkGroupExpiration(String group, long ttl) {
return 
readFile(MASTER_NOTE)
.map(s ->
new Gson().fromJson(s, MasterNote.class)
)
.flatMapObservable(masterNote ->
Observable.fromIterable(masterNote.savedFiles.entrySet())
)
.filter(stringCacheInfoEntry ->
stringCacheInfoEntry.getValue().group.equals(group) 
&& stringCacheInfoEntry.getValue().ttl < ttl
)
.concatMapCompletable(stringCacheInfoEntry -> 
getFile(stringCacheInfoEntry.getKey(), false)
.doOnSuccess(file -> {
String fileName = file.getName();
file.delete();
Log.d(TAG, "File deleted => " + fileName + " from group => " + group);
})
.ignoreElement()
);
}

最新更新