roughly WorkManager in multi-module apps. Modularization is a good instrument for… | by TJ | Android Builders | Mar, 2023 will lid the newest and most present advice roughly the world. get into slowly because of this you perceive nicely and appropriately. will deposit your data precisely and reliably
Modularization is a good instrument for constructing an utility at scale, nonetheless care should be taken to make sure that assets shared between modules are instantiated appropriately. WorkManager
gives default settings which can be utilized whenever you begin the appliance. That is normally handy, however in a modularized utility the place completely different modules usually have completely different dependencies, WorkManager’s
the default settings will not be sufficient. This weblog submit particulars how we arrange WorkManager
to make use of in multi module Now in android app.
WorkManager
exists as a singleton occasion. To offer customized configuration for this occasion in a single-module utility, you’ll be able to implement a Configuration.Supplier
which gives parameters for the Executor
used to carry out the job. You may as well implement a WorkerFactory
to help in dependency injection when utilizing WorkManager
with grip.
Issues get extra nuanced in a multi-module app. Which module must be answerable for configuring the WorkManager
occasion? If such a module exists, would not it should depend upon all of the modules that want employees? Would not that break the precept of “low cohesion” when modularizing purposes? It could, so as a substitute, we leaned on one other precept of software program engineering: delegation.
Making a DelegatingWorker
WorkManager
WorkerFactory
situations can create Employee
given runtime situations:
Whenever you mix this with Hilt’s entry factors, it’s doable to dynamically delegate to Employee
situations lazily with out having to implement the Configuration.Supplier
in a central location. The skeleton for such an implementation is as follows:
The above code determines what Employee
the appliance should delegate. To take action, it reads the totally certified title of the Employee
class of the WorkerParameters
. Upon getting that, take the HiltWorkerFactoryEntryPoint
of the appliance Context
and makes use of it to instantiate the given Employee
.
See the next snippet for an instance of a utility technique you possibly can use to move the total title of the employee to the DelegatingWorker
By the WorkerParameters
:
To make use of the above operate, create a WorkRequest
pointing at DelegatingWorker
after which add every other metadata wanted for the job within the delegate Employee
. Within the snippet that follows, the delegate Employee
is he SyncWorker
.
Lastly, queue the job as typical:
use of a DelegatingWorker
The occasion is helpful in additional than simply multi-module purposes. It is usually helpful for libraries that want to make use of WorkManager
however they can’t simply move their dependencies to the purposes that depend upon them.
A useful rule of thumb is that if you do not have handy entry to the Software
occasion within the utility and it’s essential name WorkManager
use a delegate employee to lazily create your present Employee
with what is important WorkerFactory
.
I hope the article not fairly WorkManager in multi-module apps. Modularization is a good instrument for… | by TJ | Android Builders | Mar, 2023 provides notion to you and is helpful for further to your data