Use Case Examples
Background Syncing
Run PowerSync operations while your app is inactive or in the background
Background Syncing
Applications often need to sync data when they’re not in active use. This document explains background syncing implementations with PowerSync.
Platform Support
Background syncing has been tested in:
- Flutter - Using workmanager
- Kotlin Multiplatform - Android - Implementation details in the Supabase To-Do List demo
These examples can be adapted for other frameworks like React Native. For implementation questions or assistance, chat to us on Discord.
Flutter Implementation Guide
Prerequisites
- Complete the workmanager platform setup
- Review the Supabase To-Do List Demo for context
Configure the Background Task
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.
Testing
Add a test button:
Press the button, background the app, wait 10 seconds, then verify new records in the remote database.
Platform Compatibility
Android
- Implementation works as expected.
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](https://github.com/fluttercommunity/ flutter_workmanager/pull/511) 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?