I am trying to verify if a list of files is downloaded (or not) in my phone, using ionic 2 and cordova Native File Plugin. I belive that my problem could be caused by inserting an asynchronous code (this.file.checkFile …) inside a synchronous section (FOR loop )
In my app I use an array (v_pdf) to store the files names that i intend to check out and another array (v_is_in_phone) which in the end will have only 0 and 1 values, 0-file not found and 1-file found.
At the begining v_is_in_phone is initialized with -1 values. In the FOR loop i change them according to the situation, with 0 or 1.
How can this be done? To have in the end an array v_is_in_phone holding only 0 and 1 values?
I have tried the code bellow but v_is_in_phone holds only -1 values after loop.
Code:
var v_pdf: string[]=["1.pdf","2.pdf","100.pdf","700.pdf"];
var v_is_in_phone: number[]=[-1,-1,-1,-1];
for (var i=0; i<v_pdf.length; i++){
var name=v_pdf[i];//name of the file
var path_in_myphone=this.file.dataDirectory; //path where my files are downoaded
this.file.checkFile(path_in_myphone, name).then( YES=>{
v_is_in_phone[i]=1;
}).catch(NO=>{
v_is_in_phone[i]=0;
})
}
Many thanks in advance.