Demo: Add example using a foreground service for synchronization #143
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In the
supabase-todolist-demo
project, this adds a new Android app that will manage the PowerSync connection task in a service. I've put this into a new app project to avoid too many changes to the existing project (it complicates the sync setup a bit) while still being able to reuse UI code with the existing demo (so we can keep maintaining this example easily).In the shared app, I've introduced Koin for dependency injection. I think it's a good fit for that demo because it suppports multiplatform (with very good support for Android too). We have to stop creating the
PowerSyncDatabase
in aremember
block of the compose component because we want to use the same instance outside of it as well. So now we use Koin and inject the database into the UI component (and into the background sync service in the new Android app).The only other change to the shared app is that we don't unconditionally call
connect()
anddisconnect()
in the auth view model anymore. The view model will still do that by default, but this behavior is disabled by the background sync app.Apart from that, the only change is starting a foreground service once the user is logged in. This keeps the app process (and thus, the connector + the streaming sync implementation) alive, allowing us to keep the synchronization active while the main app is closed.
It looks like there's almost nothing we could do in the SDK to make this easier. Users create a singleton of their database with whatever DI framework they're using, and then they can connect in a background service without much additional setup.
Demo video
Bildschirmaufnahme.2025-03-13.um.10.11.48.mov
After closing the app and removing it from the recently used apps (destroying the activity), the sync client keeps running. I can change a row in supabase while the app is closed, turn on airplane mode and open the app - the changes were already synced!
I've also tested this on an Android 9 emulator.