Get started
Swift SDK
Fetch data

Pagination and safe writes

Pagination and safe writes

A listing reads as one async sequence. A write can be repeated without landing twice, and can refuse to overwrite a change.

Read a whole listing

Every listing has a walk beside it, walk before the operation's name. It asks for each page as you read its rows:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

let itineraries = try mountaya.studio.itineraries.walkGetItineraries(
  query: .init { $0.sort = .updatedAtDescending }, pageSize: 50, maxPages: 10)
for try await itinerary in itineraries {
  print(itinerary.name)
}

Break out of the loop to stop asking. PageWalk walks any listing the same way, a call of your own included.

Retry a write safely

Every write sends an Idempotency-Key, the same one on each attempt, so a repeated write lands once. Generate your own and keep it with the work, to send it again when you try again later:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

// Generated once, and stored with the itinerary you are about to create.
let key = UUID().uuidString
let body = RestSchema.ItineraryCreateParams(
  activity_id: "a1c4e7b2-3d59-4f08-8b6a-2e5c9d0f7134", is_private: true,
  name: "Tour du Mont-Blanc")

let created: Response<RestSchema.ItineraryWithDetails, [String: JSONValue]?>
do {
  created = try await mountaya.studio.itineraries.postItinerary(body: body, idempotencyKey: key)
} catch MountayaError.connection {
  // No answer: send the same write with the same key, and it lands once.
  created = try await mountaya.studio.itineraries.postItinerary(body: body, idempotencyKey: key)
}
print(created.status, created.data.name)

Write only if nothing changed

Send the tag you read the resource under. The write is refused if someone changed it since:

import Foundation
import MountayaClient

let environment = ProcessInfo.processInfo.environment
let mountaya = try Mountaya(
  credential: .publishable(
    key: environment["MOUNTAYA_PUBLISHABLE_KEY"] ?? "",
    sessionToken: .value(environment["MOUNTAYA_SESSION_TOKEN"] ?? "")))

let id = "0b6d1a4e-6d3f-4f7a-9c2e-1f0a5b8c3d21"
let read = try await mountaya.studio.itineraries.getItinerary(itinerary_id: id)
do {
  _ = try await mountaya.studio.itineraries.patchItinerary(
    itinerary_id: id, body: .init { $0.name = "Tour du Mont-Blanc, summer" },
    ifMatch: read.data.etag)
} catch MountayaError.api(let failure) where failure.kind == .preconditionFailed {
  // Someone changed it since: read it again, and decide with what is there now.
  let current = try await mountaya.studio.itineraries.getItinerary(itinerary_id: id)
  print("Changed since you read it; it is now named", current.data.name)
}

Next steps

Last updated