CachedDataSourceAdapter
CachedDataSourceAdapter
The CachedDataSourceAdapter
is an adapter that the library will use to cache the network entities in the DataSource
.
The adapter has four methods that the user has to implement:
getDataSourceFactory
: will be used to list the cached elements.The returned value is used to create the
LivePagedListBuilder
.runInTransaction
will be used to apply multipleDataSource
operations in a single transaction. That means that if something fails, all operations will fail.saveEntities
will be invoked to save all entities returned by theNetworkDataSourceAdapter
into theDataSource
.This will be executed in a transaction.
dropEntities
will be used to delete all cached entities from theDataSource
.This will be executed in a transaction.
DataSource
The implementation of the CachedDataSourceAdapter
will depend mostly on the DataSource
that we choose. We will use the Room Persistence Library which provides a DataSource
trivially. However, you could use any other DataSource
.
Save and retrieve data
The DataSource
should return the entities in the same order as provided by the service.
There are at least two approaches we can follow to solve this problem:
Add an index to the entity.
Add a secondary entity to save the index.
Add an index to the entity
The first approach consists of adding an index position to the entity. Then, when a new page comes, you have to search for the biggest index in the DataSource
and then adding to all new entities an index value, starting from that index plus one.
Using Room we have to define four methods in the Entity
Dao
, to provide the CachedDataSourceAdapter
.
insertEntities
: A method to insert the entities.getEntities
: A method to retrieve all entities sorted by an index.getNextIndex
: A method to return the next index value.deleteEntities
: A method to delete the entities.
Using the EntityDao
, the CachedDataSourceAdapter
is:
Add a secondary entity to save the index
Although the previous approach works in some cases, there are other complex cases in which it will not work. Suppose there are multiple services that could return the same entities but in different order. In this case, using the current approach, we have to add two position indexes to the Entity
. It will work, but it's not an elegant solution. We are adding some logic to an entity that is not directly connected to it. Furthermore, suppose that you need to save some parameter, for example a filter field: you will not be able to model it with a single entity.
The second approach to solve the problem is having multiple objects to model the situation. One object to model the entity itself and one object for each relationship or ordering of this entity. In the last model category you can also save the parameter list, such as the listing filters or sort parameters which were used to make the service request.
The EntityDao
is:
Using the EntityDao
, we can implement the CachedDataSourceAdapter
. Some considerations to take into account when we are implementing the CachedDataSourceAdapter
are:
The
saveEntities
method has to save the entities and the related entities.The
dropEntities
method can delete both entities or just drop the related entity.If there are multiple services that can return the same entities, we should take some considerations to delete both entities and keep the
DataSource
consistency.
One entity vs Multiple entities
Although we have presented two approaches, we recommend using the second one. It's a bit harder to implement than the other one, but it has its advantage. In both approaches you have to remember the extra steps to take before dropping the entities. However, the second one provides an easier and more flexible way to implement them.
Last updated