0

I need to perform long running task. In that task I would copy something from one place to another.

I've tried to use IntentService for that, but the problem is that it can be killed and I will lose all work in that case. I know that I could tell the system that I need to restart it ASAP, and the service could know about it, but I need to start work exactly from where I was left.

It's not so difficult to do that in "stupid way". Say I use some persistent storage to store some "pointer" to the point where I left and somehow start working from that point.

But the thing is I would need to do a lot of work to do that. Like recompute progress to display in notification. Also it would require serious rewrite of the code which now looks like that.

public class Exporter implements ElementExporter {
    @Override
    public void exportElements(ReadableElementStorage from, WritableElementStorage to) throws IOException {
        while (from.hasNextElement()) {
            Element element = from.loadElement();
            to.saveElement(element);
        }
    }
}

I think that there should be some framework to do long running task, which could be cancelled and restarted after some crash. Or maybe you would suggest a better design?

user1685095
  • 5,787
  • 9
  • 51
  • 100
  • 2
    Any solution you can implement in Android, can be interrupted say when phone powers down. So your app needs to detect somehow that it was interrupted in the last session, and continue from that point. And yes, I would store the progress in persistent storage, such as SharedPreferences. I do not know any library that you could use for that. Seems that you need to implement state saving/restoring for the entities you're copying – Mixaz Nov 29 '14 at 20:28
  • Well... That's sucks. I can of course do it ad hoc. But I think that the problem cries for the general solution and it's shame no one did that. Well, maybe I will. – user1685095 Nov 29 '14 at 20:33
  • @Mixaz And Jesus, don't use shared preferences to store data. It' for preferences :-). Use a key value storage or something :-) – user1685095 Nov 29 '14 at 20:36
  • Googling for "android key value storage" shows http://developer.android.com/training/basics/data-storage/shared-preferences.html as the first result )) Let me know if you find something other in Android for that matter – Mixaz Nov 29 '14 at 21:22
  • @Mixaz Yeah, I know. You can use it like that. But it should have been called shared key value store or something. Besides it's clearer to use some storage and mappers. But that's my personal preference. – user1685095 Nov 29 '14 at 21:26
  • http://stackoverflow.com/a/27165434/2572341. Check this out – Dexter Nov 29 '14 at 21:49

0 Answers0