TheArchitect123/KmpEssentials

[Android] [Backgrounding & Workers] WorkManager and Context

Closed this issue · 1 comments

Hello.

I looked at the implementation of WorkManager and something confused me.

We pass code to WorkManager directly, but usually DI is used for this. Won't your implementation leak memory?

For example, if I have a dependency that uses Application Context, and WorkManager lives longer than Application and can run for 10 minutes after Application is closed - doesn't this indicate that such interaction will lead to memory leak in your implementation?

I first saw that executable code was passed to WorkManager using lambda. I have never seen such implementations.

KmpBackgrounding.createAndStartWorker(null){
    // my action to run in the background
    // this action will run even if the device is locked
}

There's a new api available if you need a background service that will immediately close after the app closes.
This avoids having to maintain a reference to ApplicationContext.

https://thearchitect123.github.io/ArtifactsDocProduction/develop/kotlin/multiplatform/kmpessentials/modules/backgrounding#run-your-action-inside-a-foreground-service-for-long-running-background-processes

KmpBackgrounding.createAndStartForegroundWorker

This will generate a foreground service with a configurable silent notification. This service runs indefinitely and is attached to the app. Once you close the app the service is immediately closed.

Worker Managers is more suitable for services you need to run beyond the application.
Though I need to see how I can still launch worker managers without an active application context. Maybe via a broadcast receiver instead.