Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cocos2dx UserDefault 和 LocalStorage 比较 #4

Open
zhishu520 opened this issue Nov 24, 2018 · 0 comments
Open

cocos2dx UserDefault 和 LocalStorage 比较 #4

zhishu520 opened this issue Nov 24, 2018 · 0 comments

Comments

@zhishu520
Copy link
Owner

zhishu520 commented Nov 24, 2018

cocos 有两种本地存储方式, 一种是UserDefault 另一种是 LocalStorage。
其中UserDefault是基于xml, 而LocalStorage 是基于的sqlite。
至于为什么要搞出两套方案,应该是jsb没办法去使用UserDefault,只能用LocalStorage。

对于使用层面上来说,两者其实拿到的都是字符串类型,然后需要根据自己的需要去进行转换成想要的数据类型,UserDefault封装了几种常用的类型,而LocalStorage是没有封装的.

最后欣赏一下代码

UserDefault 的 字符串 数据的存储读取

string UserDefault::getStringForKey(const char* pKey, const std::string & defaultValue)
{
    const char* value = nullptr;
    tinyxml2::XMLElement* rootNode;
    tinyxml2::XMLDocument* doc;
    tinyxml2::XMLElement* node;
    node =  getXMLNodeForKey(pKey, &rootNode, &doc);
    // find the node
    if (node && node->FirstChild())
    {
        value = (const char*)(node->FirstChild()->Value());
    }

    string ret = defaultValue;

    if (value)
    {
        ret = string(value);
    }

    if (doc) delete doc;

    return ret;
}


static void setValueForKey(const char* pKey, const char* pValue)
{
     tinyxml2::XMLElement* rootNode;
    tinyxml2::XMLDocument* doc;
    tinyxml2::XMLElement* node;
    // check the params
    if (! pKey || ! pValue)
    {
        return;
    }
    // find the node
    node = getXMLNodeForKey(pKey, &rootNode, &doc);
    // if node exist, change the content
    if (node)
    {
        if (node->FirstChild())
        {
            node->FirstChild()->SetValue(pValue);
        }
        else
        {
            tinyxml2::XMLText* content = doc->NewText(pValue);
            node->LinkEndChild(content);
        }
    }
    else
    {
        if (rootNode)
        {
            tinyxml2::XMLElement* tmpNode = doc->NewElement(pKey);//new tinyxml2::XMLElement(pKey);
            rootNode->LinkEndChild(tmpNode);
            tinyxml2::XMLText* content = doc->NewText(pValue);//new tinyxml2::XMLText(pValue);
            tmpNode->LinkEndChild(content);
        }    
    }

    // save file and free doc
    if (doc)
    {
        doc->SaveFile(FileUtils::getInstance()->getSuitableFOpen(UserDefault::getInstance()->getXMLFilePath()).c_str());
        delete doc;
    }
}

LocalStorage 的存储与读取

/** sets an item in the LS */
void localStorageSetItem( const std::string& key, const std::string& value)
{
    assert( _initialized );
	
    int ok = sqlite3_bind_text(_stmt_update, 1, key.c_str(), -1, SQLITE_TRANSIENT);
    ok |= sqlite3_bind_text(_stmt_update, 2, value.c_str(), -1, SQLITE_TRANSIENT);

    ok |= sqlite3_step(_stmt_update);
	
    ok |= sqlite3_reset(_stmt_update);
	
    if (ok != SQLITE_OK && ok != SQLITE_DONE)
        printf("Error in localStorage.setItem()\n");
}

/** gets an item from the LS */
bool localStorageGetItem( const std::string& key, std::string *outItem )
{
    assert( _initialized );

    int ok = sqlite3_reset(_stmt_select);

    ok |= sqlite3_bind_text(_stmt_select, 1, key.c_str(), -1, SQLITE_TRANSIENT);
    ok |= sqlite3_step(_stmt_select);
    const unsigned char *text = sqlite3_column_text(_stmt_select, 0);

    if (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
    {
        printf("Error in localStorage.getItem()\n");
        return false;
    }
    else if (!text)
    {
        return false;
    }
    else
    {
        outItem->assign((const char*)text);
        return true;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant