Android-cleanarchitecture: What about offline first, live local datastore updates

Created on 8 Aug 2016  Â·  1Comment  Â·  Source: android10/Android-CleanArchitecture

Hi all.

Alright in my project I followed an offline first architecture where I'm always Querying the local datastore. For each ressources that requires query, a Presenter has two UseCaseobjects:

  1. One that makes requests to the remote data store and save it locally, it never deliver information to presenter, the rx.Subscriber only does Logging. All saving operations are performed in doOnNext.
  2. Another one that is directly subscribed to the local dataStore and listen for insertion/deletion/updates via an Observable so that once something is changed it queries data and present it to the view.
 final FetchArticleCollectionUseCase mFetchArticleCollectionUseCase;
 final GetArticleUpdatesUseCase      mGetArticleUpdatesUseCase;

It's working pretty well now. But I'm wondering if I'm still making a good use of UseCase. And I personally find it cumbersome to write two UseCase for one data output every time.

discussion question

Most helpful comment

You shouldn't write Use Cases for this. Use cases are meant for Business logic (what does your app actually do?). This kind of functionality belongs in the Data Layer, as this functionality is about "where do i get my data from". A Use Case doesn't care where the data comes from.

In the case of the Repository pattern, it is the Repository that decides where the data comes from. So a Use case would query the Repository for data, and the Repository goes to check if a local copy is available and pass that. It not, it first gets data from the network, stores it locally and passes it on. This is all it does.

Cache validation (how long is your local data correct?) is the responsibility of the Data Store. There you define how long your data is valid. The Repository would ask the local Data Store if the data is there and if it is valid and then act on it. If it is valid, just return what the local Data Store has. Otherwise the Repository creates a Network Data Store and uses that to get the data and save it to the local Data store.

>All comments

You shouldn't write Use Cases for this. Use cases are meant for Business logic (what does your app actually do?). This kind of functionality belongs in the Data Layer, as this functionality is about "where do i get my data from". A Use Case doesn't care where the data comes from.

In the case of the Repository pattern, it is the Repository that decides where the data comes from. So a Use case would query the Repository for data, and the Repository goes to check if a local copy is available and pass that. It not, it first gets data from the network, stores it locally and passes it on. This is all it does.

Cache validation (how long is your local data correct?) is the responsibility of the Data Store. There you define how long your data is valid. The Repository would ask the local Data Store if the data is there and if it is valid and then act on it. If it is valid, just return what the local Data Store has. Otherwise the Repository creates a Network Data Store and uses that to get the data and save it to the local Data store.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StreetFlaneur picture StreetFlaneur  Â·  3Comments

ZherebtsovAlexandr picture ZherebtsovAlexandr  Â·  3Comments

jiaju-yang picture jiaju-yang  Â·  5Comments

Cook1 picture Cook1  Â·  4Comments

donlucard picture donlucard  Â·  3Comments