2013-05-02 141 views
0

我真的花了最后6个小时尝试使用phonegap保存数据,我的第一种方法是使用localStorate API,但每次应用程序重新启动时都会被终止,因此无用。现在我实现了它写一个文件到文件系统,但有以下问题:Phonegap中的永久存储

  • 该文件被创建OK
  • 文件具有corrent内容做
    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" 
    }); 
} 

回答

2

从我可以看到你已经忘记了一个步骤中的文件的读取操作。

你的情况,你有这样的顺序:

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种不同的文件/数据存储选项,并可在多种平台上使用。

+0

谢谢!我不知道我是如何错过的:/ – raulriera 2013-05-04 00:33:43

2

作为一个说明,在cordova(phonegap)版本2.6中存在localstorage持久性问题,因此如果这是您的情况,请尝试迁移到最近发布的cordova 2.7或降级到2.5以摆脱所述错误。

+0

希望我可以将你的答案都标记为正确:)谢谢,那肯定会解决我以前的方法中的问题。 – raulriera 2013-05-04 00:34:07