Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Room - check if data were fetched recently

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?

like image 691
Francis Avatar asked Jun 18 '18 12:06

Francis


People also ask

What is RoomDatabase?

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.

What is Room persistence library?

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.


1 Answers

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.

like image 180
Nym1412 Avatar answered Sep 22 '22 17:09

Nym1412