Skip to main content

General principles

Deferred results

Some SDK methods (e.g., those that access a remote server) return deferred results (Future). To work with them, create a data obtaining handler and an error handler.

Example of obtaining an object from the directory:

// Create an object for directory search
val searchManager = SearchManager.createOnlineManager(sdkContext)

// Get the directory object by its identifier
val future = searchManager.searchByDirectoryObjectIds(listOf(objectId))

// Process the result
future.onResult { directoryObjects ->
val directoryObject = directoryObjects.firstOrNull() ?: return@onResult
Log.d("APP", "Object title: ${directoryObject.title}")
}

// Process the error
future.onError { error ->
Log.d("APP", "Error occurred when retrieving information about the object.")
}

By default, results are processed in the UI thread. To change this, specify Executor for both onResult and onError functions.

For more information on working with the object directory, see the Directory section.

Data channels

Some SDK objects provide data channels that can be processed. To subscribe to a data channel, specify a handler function. You can unsubscribe from a channel when data processing is no longer required. To work with the data channels, use the Channel interface.

Example of subscribing to change of the visible map area (channel of new rectangular areas):

// Subscribe and process the results in the main thread
// Values will be sent on any change of the visible area until unsubscribed
// It is important to keep the connection to the channel, otherwise the subscription will be destroyed
val connection = map.camera
.statefulChanges(CameraChangeReason.STATE) { map.camera.visibleRect }
.connect { geoRect ->
Log.d("APP", "South-west point: ${geoRect.southWestPoint.latitude.value}")
}

To avoid memory leaks, unsubscribe from a channel after finishing working with it:

connection.close()