-
Notifications
You must be signed in to change notification settings - Fork 74
Sync
The Sync feature plays a pivotal role in ensuring that your diary app seamlessly integrates with Google Drive / Dropbox, allowing users to effortlessly manage their notes across multiple devices.
Here's an in-depth explanation of how it works:
1. Hash-Based Note Comparison
Each note within the app is associated with a unique hash value. The hash value is SHA1 hash of note's title + note's body + note's created_at timestamp. This hash serves as a digital fingerprint, allowing us to quickly determine whether a note has been altered.
2. Initial Cloud Upload and Index File Creation
- During the initial upload of data to the cloud, an index file is generated and stored in the cloud. This index file, in the form of a text file, contains vital information such as the note's ID, its hash value, creation timestamp, last modification timestamp, and a flag indicating whether the note has been deleted.
- The app then compares this cloud-based index file with the local Notes table. If any note IDs present in the cloud's index are missing locally, the app initiates a download operation to fetch these missing notes from the cloud.
- Conversely, if a note ID exists locally but not in the cloud, the app uploads it to the cloud.
- When a note ID is found in both the local and cloud indexes, and their respective hash values differ, the app uses the timestamps of the notes' last modifications to determine the appropriate action. If the local version is more recent, it gets uploaded to the cloud; if the cloud version is newer, it gets downloaded to the local device.
- If a note ID exists both locally and in the cloud, and their hash values are identical, no further action is taken, as the notes are already synchronized.
3. Ensuring Atomic Operations
All synchronization operations are designed to be atomic. This means that even if a user encounters a sudden loss of internet connectivity during the sync process, it will not result in an unstable or inconsistent state in either the local or cloud storage.