2016-05-12 22 views
0

我有一個最小的應用程序使用果醬核心。它創建一個webview,然後發送數據以使用s3eSecureStoragePut & s3eSecureStorageGet進行存儲和檢索。Marmalade核心存儲(s3eSecureStoragePut&s3eSecureStorageGet)不會持續

這工作正常,直到我關閉應用程序並重新打開它。再次加載後,我嘗試加載它返回的數據爲空白。有沒有人有任何想法,爲什麼這可能會發生?它內置/部署使用GCC ARM調試爲Android

這裏是我的C++

#include "s3e.h" 
#include "s3eDevice.h" 
#include "IwDebug.h" 
#include "s3eWebView.h" 
#include "IwGx.h" 
#include "Iw2D.h" 
#include <string> 
#include <sstream> 

s3eWebView* webView; 
bool pageLoaded; 
const char* rom = "rom://index.html"; 

struct SStoreData { 
    std::string key; 
}; 

static int loadedCallback(s3eWebView* instance, void* sysData, void* userData) { 
    pageLoaded = true; 
    return 1; 
} 

static int javaScriptCallback(s3eWebView* instance, void* systemData, void* userData) { 
//cast the data to string for easier handling 
std::string data = (char*) systemData; 
char jsFunc[128]; 
//check if page has loaded to prevent errors 
if (pageLoaded) { 
    if (!std::strncmp(data.c_str(), "saveKey", std::strlen("saveKey"))) { 
     std::string key = data.substr(data.find("|") + 1, data.length()); 
     SStoreData *toSave = new SStoreData; 
     toSave->key = key; 
     s3eSecureStoragePut (toSave, sizeof (*toSave)); 
    } else if ("loadKey" == data) { 
     SStoreData *toLoad = new SStoreData; 
     s3eSecureStorageGet (toLoad,sizeof(*toLoad)); 
     sprintf(jsFunc, "dataLoaded(\"%s\", \"key\");", toLoad->key.c_str()); 
     s3eWebViewSendJavaScript(webView, jsFunc); 
    } else { 

     IwTrace(JS_CALLBACK, ("DID NOT RECEIVE ANYTHING")); 
    } 
} 
return 1; 
} 


void init() { 
//init vars 
webView = s3eWebViewCreate(false); 
pageLoaded = false; 

//register callbacks 
s3eWebViewRegister(S3E_WEBVIEW_FINISHED_LOADING, loadedCallback, NULL, webView); 
s3eWebViewRegister(S3E_WEBVIEW_FROM_JAVASCRIPT, javaScriptCallback, NULL, webView); 

//navigate to the webpage 
s3eWebViewNavigate(webView, rom); 
//display the webview 
s3eWebViewShow(webView, 0, 0, IwGxGetDisplayWidth(), IwGxGetDisplayHeight()); 
} 

void destroy() { 
if (webView) { 
    s3eWebViewDestroy(webView); 
    webView = NULL; 

    //unregister callbacks 
    s3eWebViewUnRegister(S3E_WEBVIEW_FINISHED_LOADING, loadedCallback, webView); 
    s3eWebViewUnRegister(S3E_WEBVIEW_FROM_JAVASCRIPT, javaScriptCallback, webView); 
} 
delete rom; 

//gives me warning so I'm leaving it out. 
//delete pipHandler; 
} 

// Main entry point for the application 
int main() 
{ 
    //Initialise graphics system(s) 
    //Initialise graphics system(s) 
    Iw2DInit(); 

    //Init 
    init(); 

    // Loop forever, until the user or the OS performs some action to quit the app 
    while (!s3eDeviceCheckQuitRequest()) { 
     //Update the input systems 
     s3eKeyboardUpdate(); 
     s3ePointerUpdate(); 


    // Your rendering/app code goes here. 


    // Sleep for 0ms to allow the OS to process events etc. 
     s3eDeviceYield(0); 
    } 

    destroy(); 

    //Terminate modules being used 

    // Return 
    return 0; 
} 

,這裏是對的WebView的HTML/JS

<!DOCTYPE html> 
<html> 
<head> 
    <title>Storage Spike</title> 
</head> 
<body onload="main();"> 
    <div>Loaded value:<span id="loadedKey">(loaded value goes here)</span></div> 
    <input type="text" id="inputKey" value="123key" placeholder="enter key to save"/> 
    <button id="saveKey">Save</button> 
    <button id="loadKey">Load</button> 
    <br> 
    <script> 
    function main() 
    { 
     var saveKey = document.getElementById("saveKey"); 
     var loadKey = document.getElementById("loadKey"); 
     var inputKey = document.getElementById("inputKey"); 

     saveKey.addEventListener("click", function() { 
     var key = inputKey.value; 
     s3e.exec("saveKey|" + key); 
     }); 

     loadKey.addEventListener("click", function() { 
     s3e.exec("loadKey"); 
     }); 

    } 

    function dataLoaded(data, type) 
    { 
     console.log(data); 
     console.log(type); 
     var loadedKey = document.getElementById("loadedKey"); 
     if(type === "key") 
     loadedKey.innerHTML = data; 
     else 
     console.log("type error"); 
    } 

    </script> 
</body> 
</html> 

任何幫助將不勝感激,

親切的問候

回答

0

問題是,你正試圖直接保存std :: string的一個實例。

一個std :: string實例不過是三個指針,它們描述了它用於存儲實際字符串數據的內存。當應用程序關閉或用於存儲數據的內存被釋放時,這些指針將失去所有含義。

您需要將實際的字符串數據保存到s3eSecureStorage以使其保留。爲此,您需要序列化std :: string實例。進行序列化的一種簡單方法是首先保存字符串的長度,然後保存實際的字符串數據。

序列化和保存字符串s3eSecureStorage,你可以這樣做:

std::string input = "exampledata"; 
uint16 inputSize = static_cast<uint16>(input.size()); 
uint16 bufferSize = static_cast<uint16>(sizeof(uint16) + inputSize); 
buffer = new char[bufferSize]; 
std::memcpy(buffer, &inputSize, sizeof(uint16)); 
std::memcpy(buffer + sizeof(uint16), input.c_str(), inputSize); 
s3eSecureStoragePut(buffer, bufferSize); 
delete[] buffer; 

而且擺脫s3eSecureStorage數據和反序列化的字符串,你可以這樣做:

std::string output; 
uint16 dataSize = 0; 
s3eSecureStorageGet(&dataSize, sizeof(uint16)); 
if (dataSize > 0) { 
    uint16 bufferSize = static_cast<uint16>(dataSize + sizeof(uint16)); 
    char* buffer = new char[bufferSize]; 
    s3eSecureStorageGet(buffer, bufferSize); 
    output.resize(dataSize); 
    std::memcpy(&output[0], buffer + sizeof(uint16), dataSize); 
    delete[] buffer; 
} 

如果你想存儲多個值,序列化需要稍微複雜一點,但基本思想是一樣的。

很顯然,當沒有任何東西保存到s3eSecureStorage時,您還需要使用某種錯誤處理。

您發佈的代碼在您的應用程序的單次執行過程中工作的原因是因爲您分配了SStoreData實例新的並且從未發佈它。當你從s3eSecureStorage加載指針時,它們仍然指向保存字符串數據的分配內存。