In official Guide to App Architecture is an exaple of repository. There is a check if an object is existing in database and if is fresh:
// check if user was fetched recently
boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
Any ideas how to implement that functionality (hasUser) for DAO?
Room database Room is a database layer on top of an SQLite database. Room takes care of mundane tasks that you used to handle with an SQLiteOpenHelper . To use Room: Create a public abstract class that extends RoomDatabase. Use annotations to declare the entities for the database and set the version number.
The Room persistence library provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of SQLite. In particular, Room provides the following benefits: Compile-time verification of SQL queries.
Most Architecture Components examples are available here.
In your case, I couldn't find the UserDao class but there is a database schema sample here. users
table has a last_update
column. So you could write UserDao like this :
@Query("SELECT COUNT(*) FROM users WHERE userId == :userId AND last_update >= :timeout)
int hasUser(int userId, long timeout)
It returns 0 if the user with id userId
is not fresh enough.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With