I have a data manager class in one of my libraries. Until now it was working with executors and had its own executor. Now the class is being converted to coroutines.
The problem arises when the manager has to initialize the data asynchronously as in below example. In order to execute suspend function, the call to suspendedInit
has to be wrapped in launch
. The issue here is that the manager has no its own coroutine scope.
There are some that I am looking at and don't like:
- passing coroutine scope into
DataManager
constructor and using it. The issue with that the the user ofDataManager
does not yet support coroutines and hence has no own scope. DataManager
implementingCoroutineScope
and creating its own scope. This is discouraged as documented byCoroutineScope
documentation.
What is the recommended solution to this problem?
class DataManager {
init {
suspendedInit()
}
private suspend fun suspendedInit() = withContext(Dispatchers.IO) {
/* some long asynchronous operations here */
}
}
Aucun commentaire:
Enregistrer un commentaire