Skip to main content
Version: v6

Optimizations

The SDK is optimized to avoid waste of resources and improve performance. You can freely choose which optimizations should be enabled or disabled.

Multiple Calls to API

The SDK prevents the user from making multiple calls to the backend. If a call is already running, the SDK merges a new request into the current one and the data is propagated to both request origins of the Call.

You can change the default behavior and force calls to always make new requests to API and never merge two requests into one by using:

ChatClient.Builder(apiKey, context).disableDistinctApiCalls().build()

If you want to control new requests to API in a more granular way, you can use the extension function:

Call<T>.forceNewRequest(): Call<T>

For example, you can use the snippet presented below for query channels request:

ChatClient.instance().queryChannels(queryChannelsRequest).forceNewRequest().enqueue { result ->
when (result) {
is Result.Success -> {
// Handle success
}
is Result.Failure -> {
// Handle error
}
}
}

The returned Call will be forced to make a new request to API.

QuerySorter

To sort your object and present then in a desired order, it is possible to choose between two implementations of QuerySorter: QuerySortByReflection and QuerySortByField.

note

QuerySortByField is the default implementation used by the SDK.

QuerySort uses reflection to find the fields used for comparison. This helps you avoid creating an extra comparator implementation to support your use case.

The drawback is that reflection is an expensive operation and this implementation will have lower performance compared to QuerySortByField.

You can also choose to not use reflection and gain performance by using QuerySortByField instead, but this requires some extra work. Your custom class needs to implement the ComparableFieldProvider interface and provide the names of the fields used to sort the list. Like the example:

public data class Channel(
var id: String = "",
var type: String = "",
var name: String = "",
var image: String = "",
[...]
var hiddenMessagesBefore: Date? = null,
val cooldown: Int = 0,
var pinnedMessages: List<Message> = mutableListOf(),
var ownCapabilities: Set<String> = setOf(),
var membership: Member? = null,
override var extraData: MutableMap<String, Any> = mutableMapOf(),
) : CustomObject, ComparableFieldProvider {

@Suppress("ComplexMethod")
override fun getComparableField(fieldName: String): Comparable<*>? {
return when (fieldName) {
"cid" -> cid
"id" -> id
"type" -> type
"name" -> name
"image" -> image
[...]
"cooldown" -> cooldown
"lastUpdated" -> lastUpdated
else -> extraData[fieldName] as? Comparable<*>
}
}
}
note

Models included in the SDK already implement the ComparableFieldProvider interface and include all the fields together with extraData.

Did you find this page helpful?