JetpackEntity
Room database entity representing a Jetpack item with offline-first sync capabilities.
This entity implements an offline-first pattern with automatic sync tracking. All local modifications are tracked via sync metadata fields, enabling bidirectional synchronization with a remote data source (e.g., Firestore, REST API).
Sync Metadata Pattern
The entity uses several fields to track synchronization state:
lastUpdated: Timestamp of the last local modification (set on every update)
lastSynced: Timestamp of the last successful sync with remote
needsSync: Flag indicating this entity has pending changes to sync
deleted: Soft delete flag (entity is hidden from UI but kept for sync)
syncAction: The type of sync operation to perform (SyncAction)
Lifecycle States
Created Locally:
needsSync = true, syncAction = UPSERT, lastUpdated > lastSynced
Updated Locally:
needsSync = true, syncAction = UPSERT, lastUpdated > lastSynced
Deleted Locally (Soft Delete):
deleted = true, needsSync = true, syncAction = DELETE
Synced:
needsSync = false, syncAction = NONE, lastSynced = current timestamp
Updated from Remote:
Upserted with remote data, lastUpdated and lastSynced updated
Usage Example
// Create new entity (needs sync)
val newJetpack = JetpackEntity(
name = "Jetpack MK-1",
price = 499.99,
userId = currentUserId,
lastUpdated = System.currentTimeMillis(),
needsSync = true,
syncAction = SyncAction.UPSERT
)
// Update existing entity
val updated = existingJetpack.copy(
price = 399.99,
lastUpdated = System.currentTimeMillis(),
needsSync = true,
syncAction = SyncAction.UPSERT
)
// Soft delete entity
val softDeleted = existingJetpack.copy(
deleted = true,
needsSync = true,
syncAction = SyncAction.DELETE,
lastUpdated = System.currentTimeMillis()
)
// Mark as synced after successful remote operation
val synced = existingJetpack.copy(
needsSync = false,
syncAction = SyncAction.NONE,
lastSynced = System.currentTimeMillis()
)See also
for database operations
for data access abstraction
for sync operation types
Properties
Timestamp (milliseconds since epoch) when this entity was last successfully synced with the remote data source. Updated after successful sync operations.
Timestamp (milliseconds since epoch) when this entity was last modified locally. Updated on every create/update operation. Used to determine if entity needs sync.
The type of synchronization action to perform during the next sync operation. See SyncAction for possible values.