For the needs of my app, I need to share some data...
# android
t
For the needs of my app, I need to share some data access logic between a
Service
and a
Worker
. For that matter I introduced a
Repository
. Because the service needs to know when data from the repository have changed, the repository exposes data as
Flow<List<Foo>>
(making it a reactive repository). Since database access can be expensive i'd like to introduce some memory cache so that either the
Service
or the
Worker
can query the current
List<Foo>
faster. Where would you put that cache ? 1. As a field in the repository itself. 2. As a field in a decorator that wraps the
Repository
. 3. In the
Service
, by consuming the flow with
broadcastIn
. 4. You wouldn't use a memory cache, as
Room
loads fast enough.
4️⃣ 1
👍 1
s
I would go with no cache at first, at least until you find out loading data from the DB is a problem 🙂
💯 2
b
If you decide you need cache, I'd put it in the repository, in the local source (if you have a local and remote). When you query the local, pull from the cache, When you update the database, update the cache. If there is no cache and you try to pull from it, pull from the database and update the cache. That way your data flow is the same, always taking data from the repository.
👏 1
r
agree, use Room with LiveData