AsyncAssetManager
An extension of the AssetManager providing asynchronous file loading methods compatible with the coroutine concurrency model.
In addition to the standard asset loading methods, AsyncAssetManager allows scheduling asset loading with loadAsync which returns a Deferred reference to the asset. The reference can be awaited to obtain a fully loaded asset instance. Any errors that occur during loading can be handled with standard Kotlin try-catch clause by using the Deferred API.
To complete the Deferred references with loaded assets, AsyncAssetManager uses LoadedCallback from the AssetLoaderParameters class. If no parameters are passed to loadAsync, they will be created with getDefaultParameters. If the manager is unable to create a default instance of the parameters, an exception will be thrown. To prevent that, register a parameter supplier for any custom loader with setLoaderParameterSupplier, or always pass non-null AssetLoaderParameters to loadAsync.
AssetErrorListener set with setErrorListener will only be invoked if an error is not associated with any asset scheduled with loadAsync. Otherwise, the error will be used to exceptionally complete the Deferred instance referencing a specific asset, and all Deferred instances of assets that depend on it.
Note that unlike AssetStorage, AsyncAssetManager still has to be updated with update until the assets are loaded.
Other than the additions and listed changes necessary to implement the Deferred support, AsyncAssetManager works like a regular AssetManager, and it can be used as a drop-in replacement. They both share the same concurrency model, which relies on thread blocking with synchronized methods. Because of that, AsyncAssetManager is not expected to be faster; in fact, the extra features add some overhead, decreasing the overall performance.
The AssetStorage is advised over an AsyncAssetManager, as it was designed to be entirely non-blocking, and fully compatible with coroutines. It offers superior performance and better coroutines support. AsyncAssetManager should be used instead only as an intermediate step during migration from an AssetManager to the AssetStorage, or if an AssetManager is strictly required by an otherwise incompatible third-party API.
Constructors
Functions
Attempts to create AssetLoaderParameters instance for the asset defined by assetDescriptor. The parameters will use default loading settings whenever possible, unless modified with setLoaderParameterSupplier.
Schedules asynchronous loading of a selected asset with T class specified by the assetDescriptor. If AssetDescriptor.params are not defined, they will be obtained with getDefaultParameters.
Schedules asynchronous loading of a selected asset with T class. path must point to an existing file representing the asset, as it will be passed to the FileHandleResolver to obtain a file reference. Loader parameters are optional, and can be used to customize loading of the asset. If no parameters are given, they will be obtained with getDefaultParameters.
Adds a custom ParameterSupplier that creates default instances of AssetLoaderParameters for the asset loader with L class. The supplier will be invoked with an AssetDescriptor each time default loader parameters are requested via getDefaultParameters.
Adds a custom ParameterSupplier that creates default instances of AssetLoaderParameters for the T asset loader with loaderClass. The supplier will be invoked with an AssetDescriptor each time default loader parameters are requested via getDefaultParameters.