SyncWorker

class SyncWorker @AssistedInject constructor(@Assisted context: Context, @Assisted workerParameters: WorkerParameters, ioDispatcher: CoroutineDispatcher, homeRepository: HomeRepository) : CoroutineWorker

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

  1. Pull Phase: Download changes from Firestore since last sync

  2. Push Phase: Upload local changes (CREATE, UPDATE, DELETE) to Firestore

  3. Progress Updates: Update foreground notification with current progress

  4. 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

context

The application context provided by WorkManager

workerParameters

Worker parameters provided by WorkManager

ioDispatcher

The IO coroutine dispatcher for performing sync operations

homeRepository

The repository that performs the actual sync logic

See also

Sync
SyncManager

Constructors

Link copied to clipboard
@AssistedInject
constructor(@Assisted context: Context, @Assisted workerParameters: WorkerParameters, ioDispatcher: CoroutineDispatcher, homeRepository: HomeRepository)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
open val coroutineContext: CoroutineDispatcher
Link copied to clipboard
val foregroundInfoAsync: com/google/common/util/concurrent/ListenableFuture<androidx/work/ForegroundInfo>
Link copied to clipboard
val id: @NonNull UUID
Link copied to clipboard
val inputData: @NonNull Data
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
@get:RequiresApi(value = 28)
val network: @Nullable Network?
Link copied to clipboard
@get:IntRange(from = 0)
val runAttemptCount: Int
Link copied to clipboard
@get:RequiresApi(value = 31)
val stopReason: Int
Link copied to clipboard
val tags: @NonNull Set<String?>
Link copied to clipboard
open val taskExecutor: @NonNull TaskExecutor
Link copied to clipboard
@get:RequiresApi(value = 24)
val triggeredContentAuthorities: @NonNull List<String?>
Link copied to clipboard
@get:RequiresApi(value = 24)
val triggeredContentUris: @NonNull List<Uri?>
Link copied to clipboard

Functions

Link copied to clipboard
open suspend override fun doWork(): ListenableWorker.Result

Performs the background data synchronization work.

Link copied to clipboard
open suspend override fun getForegroundInfo(): ForegroundInfo

Provides foreground information for the sync worker with default progress.

Link copied to clipboard
override fun getForegroundInfoAsync(): com/google/common/util/concurrent/ListenableFuture<androidx/work/ForegroundInfo>
Link copied to clipboard
override fun onStopped()
Link copied to clipboard
suspend fun setForeground(foregroundInfo: ForegroundInfo)
Link copied to clipboard
fun setForegroundAsync(foregroundInfo: @NonNull ForegroundInfo): @EnhancedNullability @R|org/jspecify/annotations/NonNull|() com/google/common/util/concurrent/ListenableFuture
Link copied to clipboard
suspend fun setProgress(data: Data)
Link copied to clipboard
open fun setProgressAsync(data: @NonNull Data): @EnhancedNullability @R|org/jspecify/annotations/NonNull|() com/google/common/util/concurrent/ListenableFuture
Link copied to clipboard
Link copied to clipboard
override fun startWork(): com/google/common/util/concurrent/ListenableFuture<androidx/work/ListenableWorker.Result>
Link copied to clipboard