mutateDocumentWithTtl

abstract suspend fun mutateDocumentWithTtl(sidOrUniqueName: String, ttl: <Error class: unknown class>, mutator: suspend (<Error class: unknown class>) -> <Error class: unknown class>?): <Error class: unknown class>

Mutate value of the SyncDocument without opening it using provided Mutator function.

The Mutator function could be invoked more than once in case of data collision, i.e. if data on backend is modified while the mutate operation is executing. It guarantees that Mutator generates new value based on latest document data.

Possible use case is to implement distributed counter:

@Serializable
data class Counter(val value: Long = 0) {
operator fun plus(x: Long) = Counter(value + x)
}

syncClient.documents.mutateDocumentWithTtl<Counter>("mydocument", ttl = 1.hours) { counter -> counter + 1 }

Return

New value of the SyncDocument as a JSON object.

Parameters

sidOrUniqueName

SID or unique name of existing SyncDocument.

ttl

Time to live from now or Duration.INFINITE to indicate no expiry.

mutator

Mutator which will be applied to document data.

This function will be provided with the
previous data contents and should return new desired contents or null to abort
mutate operation. This function is invoked on a background thread.

Throws

TwilioException

When error occurred while mutating the document.