Search code examples
flutterdartcycle

Make sure a function ends before the app close - Flutter


I'm making an app using flutter, that has a cache Database. The update process Truncates the tables before inserting new data from the server.

Currently, If I close the app while the update function is running, The update is interrupted in middle and the data is not completely updated.

How Can I avoid this?


Solution

  • Use an application_state flag

    You can create a table that doesn't get truncated with the update a call it migrations or updates .
    Before you start an update insert a record in that table with a flag called application_state with value updating.
    When your update is completed change it's value to updated.
    Whenever you open your app , see if application_state is updating , to rerun your update again because it failled .
    Note that you can use this table to track user updating too, using timesteps or some other logic ...