Skip to content

Commit 4765bb2

Browse files
cleanup
1 parent 2b4fc91 commit 4765bb2

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

Sources/PowerSync/attachments/Attachment.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/// Enum representing the state of an attachment
22
public enum AttachmentState: Int {
3-
/// The attachment is queued for download
3+
/// The attachment has been queued for download from the cloud storage
44
case queuedDownload
5-
/// The attachment is queued for upload
5+
/// The attachment has been queued for upload to the cloud storage
66
case queuedUpload
7-
/// The attachment is queued for deletion
7+
/// The attachment has been queued for delete in the cloud storage (and locally)
88
case queuedDelete
9-
/// The attachment is fully synced
9+
/// The attachment has been synced
1010
case synced
11-
/// The attachment is archived
11+
/// The attachment has been orphaned, i.e., the associated record has been deleted
1212
case archived
1313

1414
enum AttachmentStateError: Error {

Sources/PowerSync/attachments/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ An `AttachmentQueue` is used to manage and sync attachments in your app. The att
2323

2424
### Example
2525

26+
See the [PowerSync Example Demo](../../../Demo/PowerSyncExample) for a basic example of attachment syncing.
27+
2628
In this example, the user captures photos when checklist items are completed as part of an inspection workflow.
2729

2830
The schema for the `checklist` table:
@@ -66,7 +68,7 @@ The default columns in `AttachmentTable`:
6668
| `has_synced` | `INTEGER` | Internal tracker which tracks if the attachment has ever been synced. This is used for caching/archiving purposes. |
6769
| `meta_data` | `TEXT` | Any extra meta data for the attachment. JSON is usually a good choice. |
6870

69-
### Steps to Implement
71+
#### Steps to Implement
7072

7173
1. Implement a `RemoteStorageAdapter` which interfaces with a remote storage provider. This will be used for downloading, uploading, and deleting attachments.
7274

@@ -179,9 +181,9 @@ let queue = AttachmentQueue(
179181
)
180182
```
181183

182-
# Implementation Details
184+
## Implementation Details
183185

184-
## Attachment State
186+
### Attachment State
185187

186188
The `AttachmentQueue` class manages attachments in your app by tracking their state.
187189

@@ -195,13 +197,13 @@ The state of an attachment can be one of the following:
195197
| `SYNCED` | The attachment has been synced |
196198
| `ARCHIVED` | The attachment has been orphaned, i.e., the associated record has been deleted |
197199

198-
## Syncing Attachments
200+
### Syncing Attachments
199201

200202
The `AttachmentQueue` sets a watched query on the `attachments` table for records in the `QUEUED_UPLOAD`, `QUEUED_DELETE`, and `QUEUED_DOWNLOAD` states. An event loop triggers calls to the remote storage for these operations.
201203

202204
In addition to watching for changes, the `AttachmentQueue` also triggers a sync periodically. This will retry any failed uploads/downloads, particularly after the app was offline. By default, this is every 30 seconds but can be configured by setting `syncInterval` in the `AttachmentQueue` constructor options or disabled by setting the interval to `0`.
203205

204-
### Watching State
206+
#### Watching State
205207

206208
The `watchedAttachments` publisher provided to the `AttachmentQueue` constructor is used to reconcile the local attachment state. Each emission of the publisher should represent the current attachment state. The updated state is constantly compared to the current queue state. Items are queued based on the difference.
207209

@@ -212,7 +214,7 @@ The `watchedAttachments` publisher provided to the `AttachmentQueue` constructor
212214
- The attachment state will be updated to `SYNCED`.
213215
- Local attachments are archived if the watched state no longer includes the item. Archived items are cached and can be restored if the watched state includes them in the future. The number of cached items is defined by the `archivedCacheLimit` parameter in the `AttachmentQueue` constructor. Items are deleted once the cache limit is reached.
214216

215-
### Uploading
217+
#### Uploading
216218

217219
The `saveFile` method provides a simple method for creating attachments that should be uploaded to the backend. This method accepts the raw file content and metadata. This function:
218220

@@ -228,7 +230,7 @@ The sync process after calling `saveFile` is:
228230
- The `AttachmentQueue` picks this up and, upon successful upload to the remote storage, sets the state to `SYNCED`.
229231
- If the upload is not successful, the record remains in the `QUEUED_UPLOAD` state, and uploading will be retried when syncing triggers again. Retries can be stopped by providing an `errorHandler`.
230232

231-
### Downloading
233+
#### Downloading
232234

233235
Attachments are scheduled for download when the `watchedAttachments` publisher emits a `WatchedAttachmentItem` not present in the queue.
234236

@@ -238,7 +240,7 @@ Attachments are scheduled for download when the `watchedAttachments` publisher e
238240
- If this is successful, update the `AttachmentRecord` state to `SYNCED`.
239241
- If any of these fail, the download is retried in the next sync trigger.
240242

241-
### Deleting Attachments
243+
#### Deleting Attachments
242244

243245
Local attachments are archived and deleted (locally) if the `watchedAttachments` publisher no longer references them. Archived attachments are deleted locally after cache invalidation.
244246

@@ -248,7 +250,7 @@ In some cases, users might want to explicitly delete an attachment in the backen
248250
- Updates the record to the `QUEUED_DELETE` state.
249251
- Allows removing assignments to relational data.
250252

251-
### Expire Cache
253+
#### Expire Cache
252254

253255
When PowerSync removes a record, as a result of coming back online or conflict resolution, for instance:
254256

0 commit comments

Comments
 (0)