Workmanager : How To Set-up Different Workmanager Configurations In Same App
Solution 1:
You can use a DelegatingWorkerFactory
and add you're custom WorkerFactory to it.
Your custom WorkerFactory will need to check if the classname passed to the factory is the one it want to handle, if not, just return null
and the DelegatingWorkerFactory
will revert to the default worker factory using reflection.
Keep in mind that you need to add your custom WorkerFactory each time you initialize WorkManager. If you don't do that and WorkManager tries to fullfill a WorkRequest for your Worker (that is normally handled by the custom WorkerFactory) it will fallback to the default WorkerFactory and fail (probably with a class not found exception).
We are using the DelegatingWorkerFactory
in IOsched, the scheduling app used for I/O and the Android Developer Summit.
The code of your custom WorkerFactory is going to be something like:
classConferenceDataWorkerFactory(
privateval refreshEventDataUseCase: RefreshConferenceDataUseCase
) : WorkerFactory() {
overridefuncreateWorker(
appContext: Context,
workerClassName: String,
workerParameters: WorkerParameters
): ListenableWorker? {
returnwhen (workerClassName) {
ConferenceDataWorker::class.java.name ->
ConferenceDataWorker(appContext, workerParameters, refreshEventDataUseCase)
else ->
// Return null, so that the base class can delegate to the default WorkerFactory.null
}
}
}
Post a Comment for "Workmanager : How To Set-up Different Workmanager Configurations In Same App"