title: Persistent storage in Phonegap
body: <p>I have literally spent the last 6 hours trying to persist data using phonegap, my first approach was using the localStorate API but that was killed everytime the app restarted so it was useless. Now I implemented it by writing a file to the the filesystem, but with the following problems:</p>
  
  <ul>
  <li>The file is created ok</li>
  <li>The file has the corrent content doing <pre>adb pull /data/data/[package name]/friends.txt)</pre> I can see the content of the file</li>
  <li>But trying to read from it I always get NULL.</li>
  </ul>
  
  <p>This is my code if anyone can help me... I am out of ideas now :(</p>
  
  <p><code>
      var friends = [];</p>
  
  <pre><code>// Initialize the Facebook SDK
  document.addEventListener('deviceready', function() {
    FB.init({
        appId: '[myAppID]',
        nativeInterface: CDV.FB,
        useCachedDialogs: false
    });
  
    // Check if we already fetched our friends
    readSavedContent();
  
  });
  
   function readSavedContent() {
      window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForRead, fail);
   }
  
   function gotFileSystemForRead(fileSystem) {
       fileSystem.root.getFile("friends.txt", {create: true, exclusive: false}, gotFileEntryForRead, fail);
   }
  
   function gotFileSystemForWrite(fileSystem) {
       fileSystem.root.getFile("friends.txt", {create: true, exclusive: false}, gotFileEntryForWrite, fail);
   }
  
    function gotFileEntryForRead(fileEntry) {
        var reader = new FileReader();
            reader.onloadend = function(evt) {
                alert("Data stored is " + evt.target.result);
            };
            reader.readAsText(fileEntry);
    }
  
   function gotFileEntryForWrite(fileEntry) {
       fileEntry.createWriter(gotFileWriter, fail);
   }
  
   function gotFileWriter(writer) {
       writer.onwriteend = function(evt) {
          // show a message
          alert(friends.length + " friends fetched, you should be able to see them if you restart the app");
       };
       writer.write(JSON.stringify(friends));
   }
  
   function fail(error) {
       console.log(error.code);
       alert(error);
   }
  
   function login() {
       FB.login(function (response) {
          console.log(JSON.stringify(response));
  
           if (response.status === "connected") {
               alert("logged in: fetching friends now");
  
               // Fetch my friends
               FB.api("/fql?q=" + encodeURIComponent('SELECT uid, first_name, last_name, email, pic, pic_big, is_app_user, devices FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1= me()) ORDER BY first_name LIMIT 2000'),
                   function(response) {
                       friends = response.data;
                       // Store the data to the disk                  
                       window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForWrite, fail);
                   }
               );
  
           } else {
               alert('not logged in');
           }
       }, {
           scope: "email"
       });
   }
  </code></pre>
  
  <p></code></p>