Skip to main content
Swift SDK / MossClient

MossClient

The entry point for the Swift SDK. Construct it with your project credentials (or a custom Authenticator), then open on-device sessions and track push jobs. All methods are async throws and dispatch native work onto a background thread. The underlying native client is thread-safe.

Example

Constructors

init(projectId:projectKey:)

Creates a client backed by a static project key.

init(projectId:authenticator:baseUrl:)

Creates a client whose bearer tokens come from a custom Authenticator. Use this in shipped apps so the long-lived project key stays on your backend.

Statics

sdkVersion

The native runtime version string.

setModelCacheDir(_:)

Override where embedding-model files are cached. You normally don’t need this - the client caches under <Library/Caches>/moss-models/ automatically on first init. Call it before constructing your first client if you need a custom location (e.g. a shared App Group container).

Methods

close()

Sends the final usage report, then frees the underlying native handle. Idempotent and safe to call while operations are in flight: it blocks until they drain, then waits up to 2 seconds for the report to arrive. Also called automatically on deinit. Sessions opened from the client stay open; close each one. Later calls throw MossError with the message MossClient already closed. See Releasing resources.

session(_:options:)

Opens an on-device MossSession. Documents are embedded locally with the bundled model (default moss-litelm on iOS) and queried without a network round-trip. Configure with SessionOptions — including autoLoadOnInit to skip the creation-time cloud load for a local-first startup.
By default the session auto-loads the named cloud index at creation. For a local-only, disk-first session, pass autoLoadOnInit: false so creation returns immediately and you control loading (restore from disk, hitting the cloud only on a miss):

createIndex(_:docs:modelId:)

Creates a cloud index from the given documents and polls until it is ready. When modelId is nil the server picks a default ("custom" when documents carry pre-computed embeddings). Returns a MutationResult.

getIndex(_:)

Gets metadata about a single cloud index. Returns an IndexInfo. Throws if the index does not exist.

listIndexes()

Lists all cloud indexes for the project. Returns an array of IndexInfo.

refreshIndex(_:)

Checks the cloud for a newer version of a loaded index and updates it in place if one exists. Returns a RefreshResult describing whether an update was applied.

loadIndex(_:options:)

Downloads a cloud index and loads it for fast local querying. Configure caching and background auto-refresh with LoadIndexOptions. Throws if the index does not exist or loading fails.

query(::options:)

Runs a semantic search against a loaded index. The index must be loaded with loadIndex(_:options:) first; querying an index that has not been loaded throws. Configure with QueryOptions and read matches from the returned SearchResult.

unloadIndex(_:)

Unloads a previously loaded index, releasing the resources it held. Subsequent query(_:_:options:) calls for that index throw until it is loaded again.

addDocs(_:docs:upsert:)

Adds or updates documents in a cloud index and polls until the rebuild completes. Returns a MutationResult.

getDocs(_:docIds:)

Retrieves documents from a cloud index. Pass docIds to fetch specific documents; omit it to fetch all of them. Returns an array of DocumentInfo.

deleteDocs(_:docIds:)

Deletes documents from a cloud index by id and polls until the rebuild completes. Returns a MutationResult.

getJobStatus(_:)

Gets the current status of an async job - for example, the job returned by MossSession.pushIndex. Poll until status is ready. Returns a JobStatus.

deleteIndex(_:)

Deletes a cloud index (e.g. one created by MossSession.pushIndex) and all its data. Returns true if deleted.

onMemoryPressure(_:)

Frees reclaimable native memory in response to an OS memory-pressure signal. Wire this from UIApplication.didReceiveMemoryWarningNotification. Returns the number of indexes freed. See MemoryPressureLevel.