Skip to content

Conversation

simolus3
Copy link
Contributor

@simolus3 simolus3 commented Mar 12, 2025

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 a remember 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() and disconnect() 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.

@simolus3 simolus3 marked this pull request as ready for review March 13, 2025 13:44
@simolus3 simolus3 requested a review from stevensJourney March 13, 2025 13:44
Copy link
Contributor

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy with the changes from my side.

@simolus3 simolus3 merged commit bca578f into main Mar 18, 2025
3 checks passed
@simolus3 simolus3 deleted the background-sync-example branch March 18, 2025 08:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants