sgrebnov / cordova-plugin-background-download

Apache License 2.0
73 stars 77 forks source link

Works only on first app run #67

Closed gspassky closed 5 years ago

gspassky commented 5 years ago

Hi there!

First of all, thank you for great plugin. It's almost fine, but... works only on first app run.

I use it to download some zip files to local storage after app is installed (to make it more lightweight). So, after one file is downloaded, the other download starts. Everything is working fine, even if I suspend app or turn on airplane mode. But if I exit app (totally exit) - on next run my code tries to download unfinished file one more time - and nothing happens. No error from your functions, simply nothing happens...

Please, help... Maybe, problem is caused by your magical solution, that allows download process to run even when app is closed (you've written this in description)?

window.downloadTopic = function(topic,version){ var fileName = topic + ".zip", uriString = "https://storage.googleapis.com/myappdownload/" + topic + "_" + version + ".zip"; window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { fileSystem.root.getFile(fileName, { create: true, exclusive: false }, function (targetFile) { var downloader = new BackgroundTransfer.BackgroundDownloader(); var download = downloader.createDownload(uriString, targetFile); app.downloadPromise = download.startAsync().then( function(){ zip.unzip(targetFile.nativeURL, targetFile.nativeURL.slice(0, -4), function(result){ if(result === -1) { window.current_topic_downloading = false; window.addStatEvent('Download: error while unzip'); } else { window.localStorage.setItem(window.current_topic_downloading+"_downloaded", 1); window.localStorage.setItem(window.current_topic_downloading+"_version", window.current_topic_downloading_version); window.current_topic_downloading = false; window.downloadNextTopic(); } }); }, function(){ window.current_topic_downloading = false; window.downloadNextTopic(); window.addStatEvent('Download: error while downloading'); }); }, function(){ window.current_topic_downloading = false; window.downloadNextTopic(); window.addStatEvent('Download: error creating local zip file'); }); }); }; window.downloadNextTopic = function(){ if(!window.current_topic_downloading && (typeof window.topics_to_download !== "undefined")) { for (let topic_cur_name in window.topics_to_download) { if(!parseInt(window.localStorage.getItem(topic_cur_name+"_downloaded")) || (parseInt(window.localStorage.getItem(topic_cur_name+"_version")) !== parseInt(window.topics_to_download[topic_cur_name]))) { window.localStorage.setItem(topic_cur_name+"_downloaded", 0); window.current_topic_downloading = topic_cur_name; window.current_topic_downloading_version = window.topics_to_download[topic_cur_name]; window.downloadTopic(topic_cur_name,window.topics_to_download[topic_cur_name]); break; } } } }; $.getJSON("downloads.json", function(result) { window.topics_to_download = result; window.current_topic_downloading = false; for (let topic_cur_name in window.topics_to_download) { if(window.localStorage.getItem(topic_cur_name+"_downloaded") == null) { window.localStorage.setItem(topic_cur_name+"_downloaded", 0); } if(window.localStorage.getItem(topic_cur_name+"_version") == null) { window.localStorage.setItem(topic_cur_name+"_version", 0); } } window.downloadNextTopic(); });

gspassky commented 5 years ago

Sorry, github doesn't correctly show the code with tabs... The code is attached... code.zip

gspassky commented 5 years ago

I'm very sorry, that was a problem of my code. Everything works perfect! Thank you very much!