0

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());
}
mantc_sdr
  • 451
  • 3
  • 17
  • What do you do with that `disposable`? – akarnokd Sep 24 '21 at 07:18
  • 1
    You shouldn't execute the subscription from your fragment. It should be done by your viewmodel or a Work. And I assume your `getAttachedDoc` returns a flowable: Room will build it as a never ending flowable giving you updates on the data so you either turn it on a single or you use `first` / `take(1)` the code you posted can't compile, what it does in the flatmap makes no sense, the comma after sendToServer and lambda specifically. – Daniele Segato Sep 24 '21 at 11:05
  • Actually was not working because of the infinite loop because of the Flowable, as you said. But it was compiling without problem, I don't understand why you said was impossible. Thanks @DanieleSegato – mantc_sdr Sep 24 '21 at 13:22
  • I dispose it at the end @akarnokd – mantc_sdr Sep 24 '21 at 13:23
  • why should'nt I subscribe in Fragment? Some google's github samples do it.. @DanieleSegato – mantc_sdr Sep 24 '21 at 13:23

0 Answers0