Reputation: 428
I have an Observable
as follows:
public Observable<List<Object>> getObjectsForUser() {
return Observable.fromCallable(() -> {
if (objects.size() < 1) {
List<ObjectDto> dtos = dataSource.getAllForUser();
if (dtos != null) {
objects = objectMapper.toDomain(dtos);
}
}
return objects;
}).subscribeOn(Schedulers.io());
}
Which is used in this way:
public Completable refreshOtherObjects() { // How do I return a completable which completes when the code in this lambda Observer has finished execution?
objectRepository.getObjectsForUser().subscribe(objects -> {
for (Object object: objects) {
List<OtherObjectDto> dtos = dataSource.getOtherObjects(object.getId());
if (dtos != null) {
otherObjectDao.upsertOtherObjects(mapper.toEntity(dtos));
}
}
});
}
My question is:
How do I return a completable which completes when the code in the lambda Observer in the last described method has finished execution?
Upvotes: 1
Views: 74
Reputation: 94871
Instead of nesting a subscribe
call inside of refreshOtherObjects
, continue the Rx chain by just doing that work inside of a .doOnNext
call, and then convert the chain to a Completable
using the ignoreElements()
operator:
public Completable refreshOtherObjects() { // How do I return a completable which completes when the code in this lambda Observer has finished execution?
return objectRepository.getObjectsForUser()
.doOnNext(objects -> {
for (Object object: objects) {
List<OtherObjectDto> dtos = dataSource.getOtherObjects(object.getId());
if (dtos != null) {
otherObjectDao.upsertOtherObjects(mapper.toEntity(dtos));
}
}
})
.ignoreElements();
}
Upvotes: 2