# Offline Data

### 1. Attach tracker to each object

Each object that has be synced, must contain following properties:

1. `createdOn`: Should be immutable, first time the object is created.
2. `updatedOn`: Should be mutable, should be set to epoch time every time this data is updated.
3. `updatedBy`: Should be mutable, this is your device's installation Id, try to store it in `SharedPreferences` or `UserDefaults`
4. `serverId` : id generated by server, should be used to update document while downloading from Online Server
5. `isDirty`: Should be mutable, this is set to `true` whenever data is updated locally.

{% @mermaid/diagram content="classDiagram
Notes --|> SyncTracker
class SyncTracker {
+long createdOn
+long updatedOn
+String updatedBy
+String serverId
+boolean isDirty
+getAsMap() : Map
}
class Notes {
...
+SyncTracker syncTracker
+getAsMap() : Map
}" %}

> If you are implementing sync for an existing data set, make sure you iterate through all of your data and mark them dirty

#### 2. Create data-sync tracker

There is also another table, it will keep track of your downloaded data. You record timestamp of last downloaded object for each table you want to sync.

{% @mermaid/diagram content="classDiagram
class DownloadedTracker {
+String tableName
+long downloadedTill
}" %}

> `DownloadTracker` table is not synced with Firestore, instead we will later use it to sync differential after we restore our DB file from AWS S3.
