mutateItem

inline suspend fun <T : Any> SyncList.mutateItem(itemIndex: Long, crossinline mutator: suspend (currentData: T) -> T?): SyncList.Item

Serializes the value returned by Mutator function into an equivalent JsonObject using a serializer retrieved from reified type parameter and uses the JsonObject to mutate value of the SyncList.Item

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.

Type T must be annotated with @Serializable.

Possible use case is to implement distributed counter:

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

list.mutateItem<Counter>(0) { counter -> counter + 1 }

Return

SyncList.Item which has been set during mutation.

Parameters

itemIndex

Index of the item to mutate.

mutator

Mutator which will be applied to list item 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

SerializationException

If the SyncList.Item.data is not a valid JSON input for the type T.

If the value returned by the Mutator cannot be represented as a valid instance of type T.

TwilioException

When error occurred while mutating the item.


inline suspend fun <T : Any> SyncMap.mutateItem(itemKey: String, crossinline mutator: suspend (currentData: T?) -> T?): SyncMap.Item

Serializes the value returned by Mutator function into an equivalent JsonObject using a serializer retrieved from reified type parameter and uses the JsonObject to mutate value of the SyncMap.Item

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.

Type T must be annotated with @Serializable.

Possible use case is to implement distributed counter:

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

map.mutateItem<Counter>("counter") { counter -> counter?.let { it + 1 } ?: Counter(1) }

Return

SyncMap.Item which has been set during mutation.

Parameters

itemKey

Key of the item to mutate.

mutator

Mutator which will be applied to map item 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

SerializationException

If the SyncMap.Item.data is not a valid JSON input for the type T.

If the value returned by the Mutator cannot be represented as a valid instance of type T.

TwilioException

When error occurred while mutating the item.