You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/PowerSync/attachments/README.md
+11-9Lines changed: 11 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,8 @@ An `AttachmentQueue` is used to manage and sync attachments in your app. The att
23
23
24
24
### Example
25
25
26
+
See the [PowerSync Example Demo](../../../Demo/PowerSyncExample) for a basic example of attachment syncing.
27
+
26
28
In this example, the user captures photos when checklist items are completed as part of an inspection workflow.
27
29
28
30
The schema for the `checklist` table:
@@ -66,7 +68,7 @@ The default columns in `AttachmentTable`:
66
68
|`has_synced`|`INTEGER`| Internal tracker which tracks if the attachment has ever been synced. This is used for caching/archiving purposes. |
67
69
|`meta_data`|`TEXT`| Any extra meta data for the attachment. JSON is usually a good choice. |
68
70
69
-
### Steps to Implement
71
+
####Steps to Implement
70
72
71
73
1. Implement a `RemoteStorageAdapter` which interfaces with a remote storage provider. This will be used for downloading, uploading, and deleting attachments.
72
74
@@ -179,9 +181,9 @@ let queue = AttachmentQueue(
179
181
)
180
182
```
181
183
182
-
# Implementation Details
184
+
##Implementation Details
183
185
184
-
## Attachment State
186
+
###Attachment State
185
187
186
188
The `AttachmentQueue` class manages attachments in your app by tracking their state.
187
189
@@ -195,13 +197,13 @@ The state of an attachment can be one of the following:
195
197
|`SYNCED`| The attachment has been synced |
196
198
|`ARCHIVED`| The attachment has been orphaned, i.e., the associated record has been deleted |
197
199
198
-
## Syncing Attachments
200
+
###Syncing Attachments
199
201
200
202
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.
201
203
202
204
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`.
203
205
204
-
### Watching State
206
+
####Watching State
205
207
206
208
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.
207
209
@@ -212,7 +214,7 @@ The `watchedAttachments` publisher provided to the `AttachmentQueue` constructor
212
214
- The attachment state will be updated to `SYNCED`.
213
215
- 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.
214
216
215
-
### Uploading
217
+
####Uploading
216
218
217
219
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:
218
220
@@ -228,7 +230,7 @@ The sync process after calling `saveFile` is:
228
230
- The `AttachmentQueue` picks this up and, upon successful upload to the remote storage, sets the state to `SYNCED`.
229
231
- 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`.
230
232
231
-
### Downloading
233
+
####Downloading
232
234
233
235
Attachments are scheduled for download when the `watchedAttachments` publisher emits a `WatchedAttachmentItem` not present in the queue.
234
236
@@ -238,7 +240,7 @@ Attachments are scheduled for download when the `watchedAttachments` publisher e
238
240
- If this is successful, update the `AttachmentRecord` state to `SYNCED`.
239
241
- If any of these fail, the download is retried in the next sync trigger.
240
242
241
-
### Deleting Attachments
243
+
####Deleting Attachments
242
244
243
245
Local attachments are archived and deleted (locally) if the `watchedAttachments` publisher no longer references them. Archived attachments are deleted locally after cache invalidation.
244
246
@@ -248,7 +250,7 @@ In some cases, users might want to explicitly delete an attachment in the backen
248
250
- Updates the record to the `QUEUED_DELETE` state.
249
251
- Allows removing assignments to relational data.
250
252
251
-
### Expire Cache
253
+
####Expire Cache
252
254
253
255
When PowerSync removes a record, as a result of coming back online or conflict resolution, for instance:
0 commit comments