Saving large amounts of data when calling onPause?

For my application, I have about 15 MB (which can be compressed with some processing power of up to about 5 MB) audio + video + image data that I need to save to disk. For example, I need to save this when the user is interrupted by a phone call (since after that the action can be killed) and when the user leaves the application.

I can save the data on the SD card in about 10 seconds if I do not compress it and something like 20 seconds, if I compress it, wherever I compress it. What options do I have to save my data when onPause is called so that I can be sure that the data has been saved?

Of some basic experiments, my activity is killed if onPause has not finished after 5 seconds. Some ideas that I had:

  • Launch a new topic in onPause and save the data there. This seems to work fine, but it looks like I shouldn't be doing it.

  • Starting the service, somehow copying the data to the service (will it be slower?), And then getting the service to save the data. I think this puts a notification icon at the top of the phone, but I don’t think it is terrible for the user to see the “Save Data ...” task here.

  • Is it possible to quickly put data into an SQL database and then save it later when the user returns to the application?

(Due to the nature of the application, there really is no practical way to save data during operation, because the user can transform the data in destructive ways using laborious operations (for example, 10 seconds for some operations) Even if I saved the original data and a list of actions performed for to recreate the data, the user will have to wait a minute or two when the application is launched to process this.)

+4
source share
1 answer

What options do I have to save my data when onPause is called so that I can be sure that the data has been saved?

Technically, what you want is impossible. After onPause() there are no guarantees.

The best answer is what @Viktor Lannér suggested. To express this in another way, do not wait for onPause() take 10-20 seconds of I / O. Come up with some kind of mechanism that allows you to save in stages, since the user performs operations as a backup mechanism, if nothing else. This is similar to how a transaction log is stored in a database.

Starting a new thread in onPause and saving data there. This seems to work fine, but it seems like I shouldn't do it.

This is dangerous because if the action closes (for example, onDestroy() will be called instantly), Android may terminate your process before the thread completes.

Starting the service, somehow copying the data to the service (will it be slower?), And then getting the service to save the data. I think this puts a notification icon at the top of the phone, but I don’t think it is terrible for the user to see the “Save Data ...” task here.

Make it an IntentService , so it will automatically shut down when work is completed. I would not “copy the data to the service”, but rather make the data centrally accessible by the static data member, if necessary. This will not automatically put a “notification icon at the top of the phone,” and for some such time it probably won't be needed.

Is it possible to quickly put data into an SQL database and then save it later when the user returns to the application?

Flash I / O is not faster for an SQL database than for anything else.

Due to the nature of the application, there really is no practical way to save data during operation, because the user can transform the data in destructive ways using laborious operations (for example, 10 seconds for some operations

Then it is probably not intended for a mobile platform. Think about whether this application is suitable for using technology.

+5
source

Source: https://habr.com/ru/post/1341355/


All Articles