【问题标题】:Persistent storage in PhonegapPhonegap 中的持久存储
【发布时间】:2013-04-27 03:58:43
【问题描述】:

在过去的 6 个小时里,我确实花了 6 个小时尝试使用 phonegap 持久化数据,我的第一种方法是使用 localStorate API,但每次重新启动应用程序时都会被杀死,所以它没用。现在我通过将文件写入文件系统来实现它,但存在以下问题:

  • 文件创建成功
  • 文件有正确的内容在做
    adb pull /data/data/[package name]/friends.txt)
    我可以看到文件的内容
  • 但试图从中读取,我总是得到 NULL。

如果有人可以帮助我,这是我的代码......我现在没有想法:(

var friends = [];

// 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"
     });
 }

【问题讨论】:

    标签: javascript android cordova local-storage


    【解决方案1】:

    据我所知,您在文件读取操作期间忘记了一个步骤。

    你的情况是这样的:

     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 is " + evt.target.result);
         };
         reader.readAsText(fileEntry);
     }
    

    什么时候应该是这样的:

    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 is " + evt.target.result);
        };
        reader.readAsText(file);
    }
    

    要了解更多信息,请查看此官方 Phonegape FileReader 文档。

    然后,您可以随时放弃此解决方案并使用 persistance js 进行数据存储。它将为您提供 4+1 种不同的文件/数据存储选项,并且适用于多种平台。

    【讨论】:

    • 谢谢!我不知道我是怎么错过的:/
    【解决方案2】:

    附带说明一下,cordova(phonegap) 版本 2.6 中存在本地存储持久性问题,因此,如果您遇到这种情况,请尝试迁移到最近发布的 cordova 2.7 或降级到 2.5 以消除上述错误。

    【讨论】:

    • 希望我能将您的两个答案都标记为正确 :) 谢谢,这肯定会解决我以前的方法的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 2021-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多