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:
- For SDK version 14.0.0 or later
- For SDK version 13.x
// 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.")
}
// Create an object for directory search
val searchManager = SearchManager.createOnlineManager(sdkContext)
// Get the directory object by its identifier
val future = searchManager.searchByDirectoryObjectId(objectId)
// Process the result
future.onResult { directoryObject ->
directoryObject ?: 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):
- For SDK version 13.0.0 or later
- For SDK version 12.x
// 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}")
}
// Choose a data channel (visible area rectangles)
val visibleRectChannel = map.camera.visibleRectChannel
// 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 = visibleRectChannel.connect { geoRect ->
Log.d("APP", "${geoRect.southWestPoint.latitude.value}")
}
To avoid memory leaks, unsubscribe from a channel after finishing working with it:
connection.close()