M
M
Mikhail_1232020-09-21 22:44:23
Android
Mikhail_123, 2020-09-21 22:44:23

How is the Repository pattern in Android?

Hello everyone :) Information in the Russian-speaking segment is a little more than nothing, help
What should I do if I want to have a choice of which source to receive data from (network or local database), I need to make two methods like "getItemsFromDb" and "getItemsFromApiSerice"? or create two different repositories, one for the network, the other for the local database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Shilovsky, 2020-09-21
@Mikhail_123

Two repositories are definitely nonsense, the repository should increase the level of abstraction, not decrease it. The essence of the repository, including logic, should not care (relatively) where the data will come from.
I do this:
One getItems(boolean needFreshData, MyCallback mCallback) method is created in the repository.
If needFreshData is true, we take the data from the api, otherwise the repository should decide for itself.
In the second case, I usually do this: I try to get it from the database, if there is nothing there - a request to the server. You can also take into account such conditions as lack of access to api and other trifles.
Also, I draw your attention to the fact that the call is asynchronous and the results will come in a callback.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question