javascript - Persistent storage in Phonegap -


i have literally spent last 6 hours trying persist data using phonegap, first approach using localstorate api killed everytime app restarted useless. implemented writing file the filesystem, following problems:

  • the file created ok
  • the file has corrent content doing
    adb pull /data/data/[package name]/friends.txt)
    can see content of file
  • but trying read null.

this code if can me... out of ideas :(

var friends = [];

// initialize facebook sdk document.addeventlistener('deviceready', function() {   fb.init({       appid: '[myappid]',       nativeinterface: cdv.fb,       usecacheddialogs: false   });    // check if 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 " + evt.target.result);           };           reader.readastext(fileentry);   }   function gotfileentryforwrite(fileentry) {      fileentry.createwriter(gotfilewriter, fail);  }   function gotfilewriter(writer) {      writer.onwriteend = function(evt) {         // show message         alert(friends.length + " friends fetched, should able see them if restart 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 friends              fb.api("/fql?q=" + encodeuricomponent('select uid, first_name, last_name, email, pic, pic_big, is_app_user, devices user uid in (select uid2 friend uid1= me()) order first_name limit 2000'),                  function(response) {                      friends = response.data;                      // store data disk                                        window.requestfilesystem(localfilesystem.persistent, 0, gotfilesystemforwrite, fail);                  }              );           } else {              alert('not logged in');          }      }, {          scope: "email"      });  } 

from can see have forgotten 1 step during file read operation.

in case have order:

 function gotfilesystemforread(filesystem) {      filesystem.root.getfile("friends.txt", {create: true, exclusive: false}, gotfileentryforread, fail);  }   function gotfileentryforread(fileentry) {      var reader = new filereader();      reader.onloadend = function(evt) {          alert("data stored " + evt.target.result);      };      reader.readastext(fileentry);  } 

when should this:

function gotfilesystemforread(filesystem) {     filesystem.root.getfile("friends.txt", {create: true, exclusive: false}, gotfileentry, fail); }  function gotfileentry(fileentry) {     fileentry.file(gotfileentryforread, fail); }  function gotfileentryforread(file) {     var reader = new filereader();     reader.onloadend = function(evt) {         alert("data stored " + evt.target.result);     };     reader.readastext(file); } 

to find out more take @ official phonegape filereader documentation.

then again can abandon solution , use persistance js data storage. provide 4+1 different options file/data storage , works on numerous platforms.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -