quangctkm9207 / mvp-android-arch-component

Android MVP sample adapts with new Android architecture components (Lifecycle, Room).
https://tinido.com
MIT License
434 stars 99 forks source link

No data loaded #1

Closed digounet closed 7 years ago

digounet commented 7 years ago

The QuestionsActivity is calling loadQuestions(false) method.

This method is loading data from local storage but there's no data.

public Flowable<List<Question>> loadQuestions(boolean forceRemote) {
Flowable<List<Question>> questions;
    if (forceRemote) {
      questions = remoteDataSource.loadQuestions(true).doOnEach(notification -> {
        // Save new data to local data source
        List<Question> list = notification.getValue();
        if (list != null && !list.isEmpty()) {
          saveDataToLocal(list);
        }
      });
    } else {
      questions = localDataSource.loadQuestions(false);
    }
    return questions;
  }

I think, it would be: if ther's no data on local storage, then load from remote.

I've tryied to change it to something like:

    override fun loadAll(forceRemote: Boolean): Flowable<List<Question>> {
        if (forceRemote){
            return loadFromRemote()
        } else {
            return localDataSource.loadAll(false)
                    .flatMap{ if (it.isEmpty()) Flowable.empty() else Flowable.fromArray(it) }
                    .switchIfEmpty(loadFromRemote())
        }
    }

But no success...

quangctkm9207 commented 7 years ago

Thanks for your nice idea. Surely, we can improve that. If you want to try it again and make PR, I highly appreciate that. I saw you are using Kotlin.

quangctkm9207 commented 7 years ago

Hi @digounet , Please check my new commit. Besides, the above logic of handling data should be decided by Presenter instead of Repository. It keeps Repository behavior consistent and decoupling with other components => easy to maintain and extend.

If you prefer to change it inside Repository with RxJava, you should try the following.

   override fun loadAll(forceRemote: Boolean): Flowable<List<Question>> {
        if (forceRemote){
           ///....
        } else {
            return localDataSource.loadAll(false)
                    .flatMap{ if (it.isEmpty()) remoteDataSource.loadAll(true) else Flowable.fromArray(it) }
        }
    }