SyncWorker
WorkManager worker that synchronizes local data with Firebase Firestore in the background.
This worker handles bidirectional data synchronization between the local Room database and remote Firestore. It runs as a foreground service with a notification, showing sync progress to the user.
Sync Process
Pull Phase: Download changes from Firestore since last sync
Push Phase: Upload local changes (CREATE, UPDATE, DELETE) to Firestore
Progress Updates: Update foreground notification with current progress
Result: Mark sync as complete or retry on failure
Foreground Service
The worker runs as an expedited foreground service to:
Show sync progress in a notification
Prevent the system from killing the process during sync
Comply with Android 12+ background execution limits
Retry Logic
Automatic retry up to TOTAL_SYNC_ATTEMPTS (3) times
Exponential backoff between retries (handled by WorkManager)
Logs retry attempts for debugging
Returns
Result.failure()after exhausting all retries
Dependency Injection
This class uses Hilt's @AssistedInject to inject dependencies into the Worker. WorkManager parameters (Context, WorkerParameters) are provided by the @Assisted annotation, while app dependencies are injected normally.
Thread Safety
All work is executed on the ioDispatcher to ensure proper threading and avoid blocking the main thread. The repository's sync method returns a Flow that emits progress updates.
Parameters
The application context provided by WorkManager
Worker parameters provided by WorkManager
The IO coroutine dispatcher for performing sync operations
The repository that performs the actual sync logic
See also
Constructors
Properties
Functions
Performs the background data synchronization work.
Provides foreground information for the sync worker with default progress.