How to check if the data in realm database is more than 5 minutes old?

390 Views Asked by At

I retrieve data from API link and save it to a realm database. Now I have to check if my data in the database older than 5 minutes. If the data is older I want to reload it.

1

There are 1 best solutions below

0
On

You have to add a field in your RealmModel (suppose you name it timestamp), then you need to set the value of this field when you retrieve any data. Then, in your RealmQuery you'll have to query the model to check values who have timestamp values that are more than 5 minutes old.

Example Query: First, we need to get the timestamp that was 5 mintues ago.

long time5MinutesAgo = System.currentTimeMillis() - 5*60*1000;

Then we can use the following query.

realm.where(ModelRealm.class).lessThan("timestamp", time5MinutesAgo).findAll();