user1361189
user1361189

Reputation: 31

Phonegap 2.1 iOS: how to write huge files with createWriter from loop of array

I try to write huge files on a iOS app (phonegap 2.1 + jquerymobile) with Phonegap getFile and createWriter functions.

In "for" loop on array that contains data JSON, i have a "getFile" function who create each file on my fileSystem directory. (that's work) In my "getFile" function, i called a success callback that call phonegap createWriter function and write the array data file. (problem is here)

My problem is that all files contains the last array data. How can i give the good data to each file?

My code :

fileSystem.root.getDirectory("directory",{create: true},
     function(entry){
              console.log('getDirectory success');
              entry.getDirectory("subdirectory", {create: true},
              function(entry){
                       console.log('subdirectory success');
                       for(var i=0, len= dataJson.length;i < len ; i++){
                           data = dataJson[i];
                           fullPath =data["data_id"]+".txt";
                           entry.getFile(fullPath, {create: true, exclusive: false},
                                        function(fileEntry){
                                                fileEntry.createWriter(
                                                     function(writer){
                                                     writer.onwriteend = function(evt) {
                                                        console.log("writer end");
                                                     }; 
                                                     writer.write(data["data_content"]);        
                                                     writer.abort();
                                                     },
                                                     fail);
                                        }
                                        ,fail);}
                       }
              },fail);
     }
     ,fail);

Thank you for your help.

Upvotes: 0

Views: 924

Answers (1)

user1361189
user1361189

Reputation: 31

I solved my problem. This code works:

fileSystem.root.getDirectory("directory",{create: true},
 function(entry){
          console.log('getDirectory success');
          entry.getDirectory("subdirectory", {create: true},
          function(entry){
                   console.log('subdirectory success');
                   for(var i=0, len= dataJson.length;i < len ; i++){
                       data = dataJson[i];
                       fullPath =data["data_id"]+".txt";
                       myGetFile(entry,fullPath,data);
                   }
          },fail);
 }
 ,fail);

function myGetFile(entry,fullPath,data){
        entry.getFile(fullPath,
                      {create: true, exclusive: false},
                     function(fileEntry){
                     myWriteFile(fileEntry,data);
                     },
                      fail);
    }
 function myWriteFile(fileEntry,data){
        fileEntry.createWriter(function(writer){
                               writer.onwriteend = function(evt) {
                               console.log("onwriteend");
                               };
                               writer.write(data);
                               writer.abort();
                               }, fail);
    }

Upvotes: 1

Related Questions