Android Resource
  • Home
  • About
  • Contact Us
  • Android
  • Extras
  • Tools & Tutorials
No Result
View All Result
Android Resource
  • Home
  • About
  • Contact Us
  • Android
  • Extras
  • Tools & Tutorials
No Result
View All Result
Android Resource
No Result
View All Result
Home Android

WorkManager in multi-module apps

vivekpanchal64@gmail.com by vivekpanchal64@gmail.com
March 13, 2023
in Android
0 0
0
0
SHARES
0
VIEWS
Share on FacebookShare on Twitter


Modularization is a great tool for building an app at scale, however care must be taken to ensure that resources shared between modules are properly instantiated. WorkManager provides a default configuration that is applied on app start up. This is often convenient, but in a modularized app where different modules often have different dependencies, WorkManager’s default configuration may not be enough. This blog post details how we set up WorkManager for use in the multi module Now in Android app.

WorkManager exists as a singleton instance. To provide custom configuration for this instance in a single module app, you can implement a Configuration.Provider that provides parameters for the Executor used to perform work. You can also implement a WorkerFactory to aid in dependency injection when using WorkManager with Hilt.

Things get more nuanced in a multi-module app. Which module should be responsible for configuring the WorkManager instance? If such a module exists, wouldn’t it need to depend on all modules that need workers? Wouldn’t that break the “low cohesion” principle when modularizing apps? It would, so instead, we lean on another software engineering principle: delegation.

Creating a DelegatingWorker

WorkManager WorkerFactory instances can create Worker instances at runtime given:

When you couple this with Hilt entry points, it becomes possible to dynamically delegate to Worker instances lazily without having to implement the Configuration.Provider in a central location. The skeleton for such an implementation follows:

The above code determines what Worker the app should delegate to. To do so, it reads the fully qualified name of the Worker class from the WorkerParameters. Once it has that, it takes the HiltWorkerFactoryEntryPoint from the application Context and uses it to instantiate the given Worker.

See the following snippet for an example of a utility method you could use to pass the fully qualified name of the worker to the DelegatingWorker via the WorkerParameters:

To use the above function, create a WorkRequest targeting the DelegatingWorker, and then add any other metadata needed for the work in the delegated Worker. In the snippet to follow, the delegated Worker is the SyncWorker.

Finally, enqueue the work as usual:

Use of a DelegatingWorker instance is useful in more than just multi-module apps. It is also useful for libraries who need to use WorkManager but cannot pass their dependencies easily to the apps that depend on them.

A useful rule of thumb is, if you do not have convenient access to the Application instance in app and you need to call on WorkManager, use a delegating worker to lazily create your actual Worker with the necessary WorkerFactory.



Source link

Related

Previous Post

Automating dependency updates in a Compose project

Next Post

Bring Your Video App to Cars

vivekpanchal64@gmail.com

vivekpanchal64@gmail.com

Next Post

Bring Your Video App to Cars

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You might also like

Bringing seamless authentication to your apps using Credential Manager API

March 30, 2023

Don’t Prewarm App Features

March 23, 2023

Now in Android #79

March 23, 2023

Compose Layouts and Modifiers: MAD Skills Wrap-Up

March 22, 2023

Mitigating soft verification issues in R8 and D8

March 20, 2023

How to leverage recent Android privacy changes to increase user trust

March 16, 2023

Blog Gallery

slide-1
v31
v27
banner
v29
v17

Tags

AIDS Apple Artificial Intelligence Branding brands causes children communities CSS domain names dot day Gaming Google Registry gTLDs humanitarian Ideas Javascript Laravel None nonprofits parent Photoshop PHP refugee Server Smartphone TLDs Tools top-level domains Tutorials Typography UI Design UN UNAIDS UNHCR UNICEF United Nations UNOCHA UN Women User Experience UX Design Web Design WHO women zero discrimination

Stay Connected

  • Home
  • About
  • Contact Us
  • Android
  • Extras
  • Tools & Tutorials

© 2023 Androidresource - Quality Android Blogs by androidresource.

No Result
View All Result
  • Home
  • About
  • Contact Us
  • Android
  • Extras
  • Tools & Tutorials

© 2023 Androidresource - Quality Android Blogs by androidresource.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In