Search code examples
androidandroid-architecture-componentsandroid-livedata

NetworkBoundResource helper class without Room


When I tried to implements the NetworkBoundResource and Resource helper class for the Room Db and Retrofit, it works perfect. However, I need to implement the Search Result from RESTful using Retrofit only without Room. The Resources class is good and I dont need to change it. What I want to do is try to remove db source inside this class.

public abstract class NetworkBoundResource<ResultType, RequestType> {
  private final AppExecutors appExecutors;

  private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>();

  @MainThread
  public NetworkBoundResource(AppExecutors appExecutors) {
    this.appExecutors = appExecutors;
    result.setValue(Resource.loading(null));
    LiveData<ResultType> dbSource = loadFromDb();
    result.addSource(dbSource, data -> {
      result.removeSource(dbSource);
      if (shouldFetch(data)) {
        fetchFromNetwork(dbSource);
      } else {
        result.addSource(dbSource, newData -> setValue(Resource.success(newData)));
      }
    });
  }

  @MainThread
  private void setValue(Resource<ResultType> newValue) {
    if (!Objects.equals(result.getValue(), newValue)) {
      result.setValue(newValue);
    }
  }

  private void fetchFromNetwork(final LiveData<ResultType> dbSource) {
    LiveData<ApiResponse<RequestType>> apiResponse = createCall();
    // we re-attach dbSource as a new source, it will dispatch its latest value quickly
    result.addSource(dbSource, newData -> setValue(Resource.loading(newData)));
    result.addSource(apiResponse, response -> {
      result.removeSource(apiResponse);
      result.removeSource(dbSource);
      //noinspection ConstantConditions
      if (response.isSuccessful()) {
        appExecutors.diskIO().execute(() -> {
          saveCallResult(processResponse(response));
          appExecutors.mainThread().execute(() ->
              // we specially request a new live data,
              // otherwise we will get immediately last cached value,
              // which may not be updated with latest results received from network.
              result.addSource(loadFromDb(),
                  newData -> setValue(Resource.success(newData)))
          );
        });
      } else {
        onFetchFailed();
        result.addSource(dbSource,
            newData -> setValue(Resource.error(response.errorMessage, newData)));
      }
    });
  }

  protected void onFetchFailed() {
  }

  public LiveData<Resource<ResultType>> asLiveData() {
    return result;
  }

  @WorkerThread
  protected RequestType processResponse(ApiResponse<RequestType> response) {
    return response.body;
  }

  @WorkerThread
  protected abstract void saveCallResult(@NonNull RequestType item);

  @MainThread
  protected abstract boolean shouldFetch(@Nullable ResultType data);

  @NonNull
  @MainThread
  protected abstract LiveData<ResultType> loadFromDb();

  @NonNull
  @MainThread
  protected abstract LiveData<ApiResponse<RequestType>> createCall();
}

Solution

  • The problem is that any loaded data have to go through the database first, then loading it from the database to the UI, as NetworkBoundResource does. Consequently, What I did is to decouple the persistent database and create a temporary field to load from.

    For example if I wanted to edit the original search method, I would suggest:

    public LiveData<Resource<List<Repo>>> search(String query) {
        return new NetworkBoundResource<List<Repo>, RepoSearchResponse>(appExecutors) {
    
            // Temp ResultType
            private List<Repo> resultsDb;
    
            @Override
            protected void saveCallResult(@NonNull RepoSearchResponse item) {
                // if you don't care about order
                resultsDb = item.getItems();
            }
    
            @Override
            protected boolean shouldFetch(@Nullable List<Repo> data) {
                // always fetch.
                return true;
            }
    
            @NonNull
            @Override
            protected LiveData<List<Repo>> loadFromDb() {
                if (resultsDb == null) {
                    return AbsentLiveData.create();
                }else {
                    return new LiveData<List<Repo>>() {
                        @Override
                        protected void onActive() {
                            super.onActive();
                            setValue(resultsDb);
                        }
                    };
                }
            }
    
            @NonNull
            @Override
            protected LiveData<ApiResponse<RepoSearchResponse>> createCall() {
                return githubService.searchRepos(query);
            }
    
            @Override
            protected RepoSearchResponse processResponse(ApiResponse<RepoSearchResponse> response) {
                RepoSearchResponse body = response.body;
                if (body != null) {
                    body.setNextPage(response.getNextPage());
                }
                return body;
            }
        }.asLiveData();
    }
    

    I ran it and it works.

    Edit: I made another simpler class to handle that (There is another answer here by Daniel Wilson has more feature and is updated).

    However, this class has no dependencies and is converted to the basics to make fetch response only:

    abstract class NetworkBoundResource<RequestType> {
    
        private val result = MediatorLiveData<Resource<RequestType>>()
    
        init {
            setValue(Resource.loading(null))
            fetchFromNetwork()
        }
    
        @MainThread
        private fun setValue(newValue: Resource<RequestType>) {
            if (result.value != newValue) {
                result.value = newValue
            }
        }
    
        private fun fetchFromNetwork() {
            val apiResponse = createCall()
            result.addSource(apiResponse) { response ->
                result.removeSource(apiResponse)
    
                when (response) {
                    is ApiSuccessResponse -> {
                            setValue(Resource.success(processResponse(response)))
                    }
    
                    is ApiErrorResponse -> {
                        onFetchFailed()
                        setValue(Resource.error(response.errorMessage, null))
    
                    }
                }
            }
        }
    
        protected fun onFetchFailed() {
        }
    
        fun asLiveData() = result as LiveData<Resource<RequestType>>
    
        @WorkerThread
        protected open fun processResponse(response: ApiSuccessResponse<RequestType>) = response.body
    
        @MainThread
        protected abstract fun createCall(): LiveData<ApiResponse<RequestType>>
    }
    

    So when using it, only one method could be implemented createCall():

    fun login(email: String, password: String) = object : NetworkBoundResource<Envelope<User>>() {
        override fun createCall() = api.login(email, password)
    }.asLiveData()