Class SingletonDelegateProperty
-
- All Implemented Interfaces:
-
kotlin.properties.ReadOnlyProperty
public final class SingletonDelegateProperty<T extends Object> implements ReadOnlyProperty<Any, T>
A delegate property that ensures a singleton instance of the given type T. The instance is created using the provided creator function and is stored as a weak reference to allow for garbage collection when not in use.
Note: Every new or different instance of
SingletonDelegatePropertywill manage a different singleton. So it is the class responsibility to wrap its instance around SingletonDelegateProperty to ensure that the singleton is managed properly.
-
-
Constructor Summary
Constructors Constructor Description SingletonDelegateProperty(Function0<T> creator)
-
Method Summary
-
-
Constructor Detail
-
SingletonDelegateProperty
SingletonDelegateProperty(Function0<T> creator)
-
-
Method Detail
-
getValue
T getValue(Object thisRef, KProperty<?> property)
Returns the singleton instance of type T. If the instance does not exist, it is created using the creator function and stored as a weak reference.
Note: The Initialization of long running constructors should be accessed from background thread which causes getValue to run on background thread.
- Returns:
The singleton instance of type T.
-
-
-
-