Background Syncing
You might want to run PowerSync operations while the client device is inactive or the app is in the background.
Flutter
We have done an initial investigation into how background syncing could be accomplished by using Flutter with workmanager. However, the flow for other frameworks (e.g. React Native) should be quite similar using an equivalent package. Please reach out on our Discord for more assistance.
Guide
We assume you have followed the platform setup guide in the workmanager README. Note we are running this in the context of our Supabase Todo List Demo app.
In main.dart
:
Note specifically in the switch statement:
- Since WorkManager executes in a new process, you need to set up the PowerSync local database and connect to the remote database using your connector.
- Run a write (in the case of this demo app, we create a ‘todo list’)
- Make sure to run
currentConnector.uploadData(db);
so that the local write is uploaded to the remote database.
Create a way to test this, e.g. by using a button:
Put your app in the background and wait 10 seconds, then check your remote database and it should have the new record.
Android
This worked with Android.
iOS
At the time of writing (January 2024), we were only able to get part of this to work using the branch for this PR into workmanager. While testing we were not able to get iOS background fetching to work, however this is most likely an issue with the package.
Was this page helpful?