0

I wish to start long-term operation, which as I think should be put into a service. Because I want to invoke it from a non-context class (Repository), I need to get somehow a context of an application. And as I also know passsing any context to the class may lead to memory leaks. So I found ContextWrapper class, which allows to use context, but I don't know is it safe to use or may be I shoud use other solution.

I am asking because I read a lot about this, but still not understand. I don`t know how to handle memory leaks, and I don't know how to determine is there a memory leak.

So my question is how to safely work with context from non-context classes, and for what reasons ContextWraper is used?

Thank you for answers.

Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
atd_motum
  • 43
  • 3
  • 1
    look at new `androidx.work.Worker`: https://developer.android.com/topic/libraries/architecture/workmanager/ – andrikeev Sep 20 '18 at 16:21
  • This simplest solution would be to access the [Application Context](https://stackoverflow.com/a/5114361/1219389), but in truth I don't believe a Repository should be launching a service - instead I would have your Repository communicate to a different part of your app, which then handles launching the Service. – PPartisan Sep 20 '18 at 16:23
  • Actually I want to start operation from onActive() in LiveData. And it is simply declared in the repository. And because Repository is instantiated on Application start, I thought it can be done here. I will try work manager. – atd_motum Sep 20 '18 at 16:41
  • @v.andrikeev yes indeed, use work manager. It will resolve most of your issues. However it is still in Alpha mode. Its methods may change promptly than any other library with its further development. – Sushant Somani Sep 21 '18 at 05:15

0 Answers0