2013-05-02 140 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