0

I have one question of best practice. I'm trying to apply Clean Architecture in a sample app using MVVM. My question is, if ViewModel is supposed to call the use cases instead of calling directly the Repository (which has access to database and the API), but I want to display the cached results while the information is being refresh, how is supposed the ViewModel to access to the cached data if it's using the use case and not the repository?

I read this post and Android official doc but I'm not sure which is the best way of achieving this behaviour.

2

1 Answer 1

1

Considering separation of concerns, I like to implement caching using decorator pattern. Depending on your concrete implementation a decorator around the repository or around the use case interactor could be a solution.

Sign up to request clarification or add additional context in comments.

2 Comments

But if you use a decorator pattern, then for example the repository would have to espose a method to retrieve the information from the cache wouldn't it? That is the use case of initialisation, when you first want to obtain the cached data
The interface of the decorator should not change. The caching repository just always returns cached data. The implementation of the caching decorator might have dependencies to an external cache implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.