PocketSync
Main orchestrator for sync operations
PocketSync
PocketSync is the main orchestrator of all synchronization operations. It exposes a singleton object PocketSync.instance
that manages initialization, user authentication, and sync control.
Overview
PocketSync follows a simple lifecycle:
- Initialize the sync engine
- Authenticate users
- Control sync operations (start/pause)
- Clean up resources when done
Initialization
Before using PocketSync, you need to initialize it with your project configuration:
Parameters
dbPath
: Path to your SQLite database fileoptions
: PocketSync configuration optionsprojectId
: Your project ID from the PocketSync consoleauthToken
: Your project’s authentication tokenserverUrl
: PocketSync server URL (defaults to https://api.pocketsync.dev)conflictResolver
: Custom conflict resolution strategy (defaults to applying remote changes)silent
: Whether to suppress console logs (defaults to true in release mode, false in debug mode)
databaseOptions
: SQLite database configurationonCreate
: Required callback to create your database schemaversion
: Database schema version number (defaults to 1)onConfigure
: Optional callback for database configuration before creation/openingonUpgrade
: Optional callback for handling database schema upgradesonDowngrade
: Optional callback for handling database schema downgradesonOpen
: Optional callback triggered after database is opened
User authentication
After initialization, you need to set a user ID to identify the current device’s owner:
This step is crucial for:
- Identifying which user’s data to sync
- Managing access control
- Tracking changes per user
Controlling sync operations
Starting sync
To begin synchronizing data:
This will:
- Connect to the PocketSync server
- Upload local changes
- Download remote changes
- Keep data in sync across devices
Pausing sync
To temporarily pause synchronization:
This is useful when you want to:
- Conserve battery/network usage
- Perform large batch operations
- Prevent immediate sync during sensitive operations
Resource cleanup
When you’re done with PocketSync (e.g., user logs out), properly dispose of resources:
This will:
- Stop all sync operations
- Close database connections
- Clean up memory resources
- Disconnect from the server
Best practices
-
Initialize early: Call
initialize()
as early as possible in your app’s lifecycle, typically inmain()
-
Handle errors: Wrap PocketSync operations in try-catch blocks:
-
Cleanup resources: Always call
dispose()
when cleaning up your app or logging out users -
User Management: Update the user ID whenever the authenticated user changes
Example implementation
Here’s a complete example showing proper PocketSync usage: