Downloading to a temp folder then overwriting original files - speed issues?

834 Views Asked by At

I have an app that downloads a single html file and various images and sometime, mp4 videos.

After an initial download, repeat downloads are done every 15 minutes by a background service.

The service checks if there are any new files and if any files have been modified (in this case, it is typically the html file that would be modified and some new images will be downloaded)

I have a webview that is displaying the html file and after the background service successfully downloads some new assets, the webview get refreshed.

However, whilst the service is downloading, the app would be using the files, displaying them in the webview (the videos are handled with a videoview, using a javascript bridge, which flips the two views around)

So, to my question.

After seeing some possible issues with the current app, I want the background service to download the assets to a temporary folder, instead of the main folder used (I use a sub-folder created in Downloads)

Then, once the downloads are complete, I would "stop" the webview, copy the files form "tmp" to the real folder, and restart the webview.

Logic sounds OK, but I am worried about speed. The files to copy could weight in at 100mb potentially (maybe more, hopefully less, due to bandwidth issues obv.) so how fast could Android copy those over?

Ideally the transition needs to happen in under a couple of seconds.

Can anyone advise on this?

Is there possibly a better way to handle this situation?

2

There are 2 best solutions below

2
On

Put the temporary directory below the main data directory, then use an atomic rename(2) call to move the new file into place. (This only works atomically when the source and destination directories are within the same filesystem, hence the recommendation about tempfile placement. You can also put them as FILENAME.EXTENSION.new into the same directory then rename them. Be wary of tempfile races, as usual, when designing filenames; use something like mkstemp(3) to create them if you can.)

4
On

Could you point the WebView at the "temporary" directory instead, and then in the background delete the original directory? (then you'd always use the "temporary" directory going forward).