SyncStack
SyncStack
SyncStack contains the state of a sync. You pass one to the sync(_:syncTypes:then:) method of Stack to say which sync to run, and the completion handler receives one for each page of the result. Use this class to
- start an initial sync, a subsequent sync, or a sync from a pagination token
- read the published, unpublished and deleted items of one page
- read the sync token to store for the next sync
Instance State
The tokens you pass to init(syncToken:paginationToken:) select the request that sync(_:syncTypes:then:) sends:
- SyncStack(), with no token, starts an initial sync. Only this form applies the syncTypes filters.
- SyncStack(syncToken:) fetches the changes since the sync that returned the token.
- SyncStack(paginationToken:) continues a sync from the page that the token names.
The SDK stops the app with a fatal error if you pass both tokens, because a sync request sends only one of them.
Class-Level Properties
| Property | Type | Access | Default | Description |
|---|---|---|---|---|
| syncToken | String | Read-only | "" | Token for the next sync. Holds a value on the last page only. |
| paginationToken | String | Read-only | "" | Token of the next page. Holds a value on every page except the last. |
| totalCount | Int | Read-only | 0 | Number of items in the whole sync, from total_count. |
| items | [Any] | Read-only | [] | Items of this page, one dictionary for each sync event. |
| endpoint | Endpoint | Read/write (static) | .sync | API path of sync requests. Leave it unchanged. |
Class-Level Notes
Empty strings, not nil. A token that the response does not include is an empty string. Check it with isEmpty.
Reading the items.
- Each element of items is a [String: Any] dictionary for one sync event, as the API returns it.
- The SDK decodes the data value of an event into an EntryModel or an AssetModel when it has the fields of one.
- Otherwise data stays a [String: Any] dictionary, for example for a deleted entry.
- Check the type of data with as? before you use it.
One object for each page. The completion handler of sync(_:syncTypes:then:) receives a new SyncStack for each page, whose items contain that page only. The Synchronization reference describes both tokens.
SyncableTypes
SyncStack.SyncableTypes specifies a filter for an initial sync. Pass an array of them as the syncTypes argument of sync(_:syncTypes:then:). The SDK sends each one as a query parameter of the Initial Sync request. The table below lists each case and the query parameter it sends.
| Case | Query parameter | Meaning |
|---|---|---|
| all | No parameter | No filter. Syncs all published entries and assets. The default. |
| contentType(String) | content_type_uid | Entries of one content type, for example .contentType("CONTENT_TYPE_UID"). |
| locale(String) | locale | Content of one locale, for example .locale("en-us"). |
| startFrom(Date) | start_from | Content published on or after a date. The SDK sends the date in ISO 8601 format, in UTC. |
| publishType(PublishType) | type | One kind of publish event, from the PublishType table below. |
A later value of the same kind replaces an earlier one in the same array, so each filter takes one value.
PublishType
SyncStack.PublishType specifies a kind of publish event for the publishType filter. Its raw value is the type value that the SDK sends. The table below lists every case.
| Case | Raw value | Meaning |
|---|---|---|
| entryPublished | "entry_published" | Published entries. |
| assetPublished | "asset_published" | Published assets. |
| entryUnpublished | "entry_unpublished" | Unpublished entries. |
| assetUnpublished | "asset_unpublished" | Unpublished assets. |
| entryDeleted | "entry_deleted" | Deleted entries. |
| assetDeleted | "asset_deleted" | Deleted assets. |
| contentTypeDeleted | "content_type_deleted" | Entries of deleted content types. |
Method Index
| Method Name | Returns | Description |
|---|---|---|
| init(syncToken:paginationToken:) | A new sync state for the given token. | Creates the state that selects the kind of sync. |
Class-Level Snippet
The snippet below runs a subsequent sync and prints the event type and title of each synced entry.
Read the entries of a subsequent sync
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.sync(SyncStack(syncToken: "SYNC_TOKEN")) { (result: Result<SyncStack, Error>) in
switch result {
case .success(let syncStack):
for case let event as [String: Any] in syncStack.items {
if let entry = event["data"] as? EntryModel {
print("\(event["type"] ?? ""): \(entry.title)")
}
}
if !syncStack.syncToken.isEmpty {
print("Next sync token: \(syncStack.syncToken)")
}
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Methods
Each method of the SyncStack class has its own section below, with its parameters, validation rules, behavior, and examples.
init(syncToken:paginationToken:)
init(syncToken:paginationToken:) creates the sync state that tells the sync(_:syncTypes:then:) method of Stack which sync to run.
| Name | Type | Description |
|---|---|---|
| syncToken | String | Sync token from the last page of an earlier sync. |
| paginationToken | String | Pagination token of the page to continue from. |
Returns: SyncStack. A new sync state for the given token.
Validation
- The SDK stops the app with a fatal error if both syncToken and paginationToken hold a value, because a sync request sends only one of them. Pass one token or none.
- There is no other client-side validation. An expired or unknown token surfaces as an APIError from the sync(_:syncTypes:then:) call that follows, not from init(syncToken:paginationToken:).
Behavior
- Client-side only. init(syncToken:paginationToken:) makes no request.
- The tokens select the kind of sync: an initial sync with no token, a subsequent sync with syncToken, or a pagination request with paginationToken. The Instance State section of the SyncStack page describes each kind, and the sync(_:syncTypes:then:) method links the API request for each.
- An empty string counts as no token.
- Read the tokens from the syncToken and paginationToken properties of the SyncStack that an earlier sync returned.
Example
Basic usage: run a subsequent sync from a stored sync token
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.sync(SyncStack(syncToken: "SYNC_TOKEN")) { (result: Result<SyncStack, Error>) in
switch result {
case .success(let syncStack):
print("\(syncStack.items.count) changes on this page")
case .failure(let error):
if let apiError = error as? APIError {
print("Error \(apiError.errorCode): \(apiError.errorMessage)")
} else {
print(error)
}
}
}Continue a sync from a pagination token
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
// Pass one token only. Passing both stops the app with a fatal error.
stack.sync(SyncStack(paginationToken: "PAGINATION_TOKEN")) { (result: Result<SyncStack, Error>) in
guard case .success(let syncStack) = result else { return }
print("\(syncStack.items.count) items on this page")
}Start an initial sync with no token
import ContentstackSwift
let stack = Contentstack.stack(apiKey: "API_KEY", deliveryToken: "DELIVERY_TOKEN", environment: "ENVIRONMENT")
stack.sync(SyncStack(), syncTypes: [.locale("en-us")]) { (result: Result<SyncStack, Error>) in
guard case .success(let syncStack) = result else { return }
print("\(syncStack.items.count) items on this page")
}