I am trying to obtain a List
of Documents, then trying to send each doc to the server and delete if from the database and phone. The thing is that it looks like it never arrives to the .subscribe
method of the Fragment
, it is like is stuck at the .flatMap
executing the sendToServer(..)
method. Any help, please?
Fragment:
disposable = mViewModel.singleSend()
.subscribe(sent -> DialogUtils.showMessage(R.string.success)
, t -> DialogUtils.showMessage(R.string.error_binding_data)
);
ViewModel:
public Single<Boolean> singleSend() {
return sendMediaAttachement()
.map(list -> !list.contains(false))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public @NonNull Single<List<Boolean>> sendMediaAttachement() {
return mRepository.getAttachedDocs(getMantenValue().getID())
.concatMap(docs -> Flowable.fromIterable(docs))
.flatMap(doc -> mRepository.sendToServer(doc, getFileFromAttachedDoc(doc)),
(doc, wsResult) -> {
if (wsResult.isSuccessful()) deleteDoc(doc);
return wsResult.isSuccessful();
})
.toList();
}
public void deleteDoc(DocumentoAdjunto doc) {
addDisposable(mRepository.delete(doc)
.subscribe(deleted -> {
if (deleted)
FileUtils.deleteFile(getFileFromAttachedDoc(doc)); //returns void
}, t -> getToastMessageInteger().setValue(R.string.file_not_deleted)));
}
Repository:
public @NonNull Flowable<WSResult> sendToServer(DocumentoAdjunto doc, File file) {
return mRemoteDataSource.uploadFile(doc, file)
.map(this::parse)
.toFlowable()
.doOnError(Timber::e)
.subscribeOn(Schedulers.io());
}
public Single<Boolean> delete(DocumentoAdjunto doc) {
return mLocalDataSource.delete(doc)
.doOnError(Timber::e)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}