I'm using cordova (phonegap) to build an Android/iOS App The App has to cache data. It receives a JSON file from a Server which is then processed (data displayed as HTML).
My problem is in this code:
function moveExercisesJSON(){
console.log("moving exercises json");
var fail = function(err) { console.log(err) }
window.resolveLocalFileSystemURL(fileSave, function(file){
window.resolveLocalFileSystemURL(store, function(store){
file.moveTo(store, "exercises.json");
console.log("done moving");
displayExerciseList();
},fail);
},fail);
}
The JSON file (exercises.json) is downloaded to a downloads-folder, then this function is opened. The file is moved via file.moveTo. Then displayExerciseList() is opened (which processes/displays the data in the json file).
However, it seems displayExerciseList() is called before file.moveTo is done. Therefor I am always one version behind when first opened (since the new version has not yet been moved from the downloads-folder)
How can I wait for .moveTo to finish before calling displayExerciseList() A timer doesn't seem to be a solution since I have to do the same for movie-files (which differ a lot in size).
Try this:
function moveExercisesJSON(){
console.log("moving exercises json");
var fail = function(err) { console.log(err) }
window.resolveLocalFileSystemURL(fileSave, function(file){
window.resolveLocalFileSystemURL(store, function(store){
file.moveTo(store, "exercises.json",function(){
console.log("done moving");
displayExerciseList();
},function(){
console.log("error moving");
});
},fail);
},fail);
}
The moveTo function has optional parameters for success and error callbacks.
For more details, you can check this page about HTML5 file API (on which is based the Cordova File plugin)