0

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    Can you not just reload the data every 5 minutes rather than checking to see if its 5 minutes old? – JakeB Aug 21 '19 at 09:43
  • My assignment says that need to check the data in local database – Dominik Tkalčec Aug 21 '19 at 09:48
  • 3
    Add a timestamp field to each row and updated when inserted/updated. Query that timestamp and pull the entire row if ```timestamp > 5 minutes``` – JakeB Aug 21 '19 at 09:51

1 Answers1

1

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();