Home > Software engineering >  Flutter: How to run remote db transactions in the background
Flutter: How to run remote db transactions in the background

Time:07-10

I have an e-commerce app. When user add/remove items I need to have them logged into my remote database. Each transaction is important and I have to make sure that I have logged them in the same order it had happened. However it slows my app and the round trip to and from database takes time.

I found out that persisting the transactions locally would be a good idea to solve the delay.Using Flutter-Hive DB my local persistence works fine and it improved the app performance.

Now I need your recommendation for sending these transactions to my back-end as and when it happens. The remote DB side of the code should run in the background without making the user wait for the confirmation. Any help, guidelines or pointers will be highly appreciated.

CodePudding user response:

you can try workmanager

void callbackDispatcher() {
  Workmanager.executeTask((task) {
    //Write codes to perform required tasks
    return Future.value(true);
  });
}

Workmanager.initialize(
    callbackDispatcher, //the top level function.
    isInDebugMode: true //If enabled it will post a notification whenever the job is running. Handy for debugging jobs
)

For periodic tasks you can use

// Periodic task registration
Workmanager().registerPeriodicTask(
    "periodic-task-identifier", 
    "simplePeriodicTask", 
    // When no frequency is provided the default 15 minutes is set.
    // Minimum frequency is 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency.
    frequency: Duration(hours: 1),
)
  • Related