WeakRef

actual class WeakRef<T : Any>(referent: T)

A weak reference to referent that does not prevent it from being collected.

MapKit does not retain the listeners passed to its subscription methods. Every subscription in this SDK therefore accepts a WeakRef instead of the listener itself, to make that contract explicit at the call site: it is your responsibility to keep a strong reference to the listener for as long as the subscription has to stay alive.

class MyScreen {
private val tapListener = MapObjectTapListener { _, _ -> true }

fun subscribe(mapObject: MapObject) {
mapObject.addTapListener(tapListener.asWeakRef())
}
}

Passing a listener that is not referenced anywhere else makes the subscription a no-op as soon as the listener is collected.

expect class WeakRef<T : Any>(referent: T)

A weak reference to referent that does not prevent it from being collected.

MapKit does not retain the listeners passed to its subscription methods. Every subscription in this SDK therefore accepts a WeakRef instead of the listener itself, to make that contract explicit at the call site: it is your responsibility to keep a strong reference to the listener for as long as the subscription has to stay alive.

class MyScreen {
private val tapListener = MapObjectTapListener { _, _ -> true }

fun subscribe(mapObject: MapObject) {
mapObject.addTapListener(tapListener.asWeakRef())
}
}

Passing a listener that is not referenced anywhere else makes the subscription a no-op as soon as the listener is collected.

actual class WeakRef<T : Any>(referent: T)

A weak reference to referent that does not prevent it from being collected.

MapKit does not retain the listeners passed to its subscription methods. Every subscription in this SDK therefore accepts a WeakRef instead of the listener itself, to make that contract explicit at the call site: it is your responsibility to keep a strong reference to the listener for as long as the subscription has to stay alive.

class MyScreen {
private val tapListener = MapObjectTapListener { _, _ -> true }

fun subscribe(mapObject: MapObject) {
mapObject.addTapListener(tapListener.asWeakRef())
}
}

Passing a listener that is not referenced anywhere else makes the subscription a no-op as soon as the listener is collected.

Constructors

Link copied to clipboard
actual constructor(referent: T)
expect constructor(referent: T)
actual constructor(referent: T)

Functions

Link copied to clipboard
actual fun get(): T?

The referent, or null if it has already been collected.

expect fun get(): T?

The referent, or null if it has already been collected.

actual fun get(): T?

The referent, or null if it has already been collected.

Link copied to clipboard

Converts this reference into the weak reference expected by the subscription methods of the original Yandex MapKit SDK.

Unwraps the referent into the object expected by the subscription methods of the original Yandex MapKit SDK, or returns null if it has already been collected.

Link copied to clipboard
inline fun <T : Any, R> WeakRef<T>.withValue(block: (T) -> R): R?

Runs block with the referent and returns its result, or returns null if the referent has already been collected.