Search code examples
androidflutterflutter-hiveairtable

How to automatically update cached data in HiveBox(Flutter) from API once a day


I use a Hive for store data in my flutter app. I faced to some problem when my remote data from API(airtable) were changed but my app is keeping to store old data. How can I update my app automaticaly per 1 day, for instance?

There are an example how I use the Hive.

await Hive.initFlutter();
var box = await Hive.openBox('wiseBox');

if(box.isEmpty) {
    //when my app was open by user at the first
    await _save();
} 
else {
    //repeatedly
    await _openCashedData();
}


_save() async {
  await box.put('data', 'some_data');
}

_openCashedData() async { // <- can be a synchronous function
  await box.get('data');
}

Solution

  • You can update your Hive data every time you start your application and when the application goes back to foreground. You store the date of the last data update and if it exceeds one day you update.

    Check this for catch when the app is "resumed"

    https://levelup.gitconnected.com/lets-utilize-the-flutter-app-lifecycle-388121533fd9