General principles | Mobile SDK | 2GIS Documentation
Android SDK

General principles

Some SDK methods (e.g., those that access a remote server) return deferred results (Future). To process a deferred result, you can register two callback functions: completion and error.

For example, to get information from object directory, you can process Future like so:

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

// Get object by identifier
val future = searchManager.searchByDirectoryObjectId(objectId)

// Completion callback
future.onResult { directoryObject ->
    Log.d("APP", "Object title: ${directoryObject.title}")
}

// Error callback
future.onError { error ->
    Log.d("APP", "An error occurred retrieving information from the directory.")
}

By default, results are processed in the UI thread. To change this, you can specify Executor for both functions.

For more information on working with object directory, see Object directory.

Some SDK objects provide data channels (see the Channel interface). To subscribe to a data channel, you need to create and specify a handler function.

For example, you can subscribe to a visible rectangle channel, which is updated when the visible area of the map is changed:

// Choose a data channel
val visibleRectChannel = map.camera.visibleRectChannel

// Subscribe to the channel and process the results in the main thread.
// It is important to prevent the connection object from getting garbage collected to keep the subscription active.
val connection = visibleRectChannel.connect { geoRect ->
    Log.d("APP", "${geoRect.southWestPoint.latitude.value}")
}

When the data processing is no longer required, it is important to close the connection to avoid memory leaks. To do this, call the close() method:

connection.close()