mirror of https://github.com/axmolengine/axmol.git
Fix local storage get item issue when item inexist
This commit is contained in:
parent
3409f0fcd4
commit
6b4f8cc0ab
|
@ -111,21 +111,25 @@ void localStorageSetItem( const std::string& key, const std::string& value)
|
|||
}
|
||||
|
||||
/** gets an item from the LS */
|
||||
std::string localStorageGetItem( const std::string& key )
|
||||
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
||||
{
|
||||
assert( _initialized );
|
||||
JniMethodInfo t;
|
||||
|
||||
std::string ret;
|
||||
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;")) {
|
||||
if (JniHelper::getStaticMethodInfo(t, "org/cocos2dx/lib/Cocos2dxLocalStorage", "getItem", "(Ljava/lang/String;)Ljava/lang/String;"))
|
||||
{
|
||||
jstring jkey = t.env->NewStringUTF(key.c_str());
|
||||
jstring jret = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID, jkey);
|
||||
ret = JniHelper::jstring2string(jret);
|
||||
outItem->assign(JniHelper::jstring2string(jret));
|
||||
t.env->DeleteLocalRef(jret);
|
||||
t.env->DeleteLocalRef(jkey);
|
||||
t.env->DeleteLocalRef(t.classID);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/** removes an item from the LS */
|
||||
|
|
|
@ -120,23 +120,26 @@ void localStorageSetItem( const std::string& key, const std::string& value)
|
|||
}
|
||||
|
||||
/** gets an item from the LS */
|
||||
std::string localStorageGetItem( const std::string& key )
|
||||
bool localStorageGetItem( const std::string& key, std::string *outItem )
|
||||
{
|
||||
assert( _initialized );
|
||||
|
||||
std::string ret;
|
||||
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 (text)
|
||||
ret = (const char*)text;
|
||||
|
||||
if( ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW)
|
||||
if( (ok != SQLITE_OK && ok != SQLITE_DONE && ok != SQLITE_ROW) || !text)
|
||||
{
|
||||
printf("Error in localStorage.getItem()\n");
|
||||
|
||||
return ret;
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
outItem->assign((const char*)text);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/** removes an item from the LS */
|
||||
|
|
|
@ -46,7 +46,7 @@ void CC_DLL localStorageFree();
|
|||
void CC_DLL localStorageSetItem( const std::string& key, const std::string& value);
|
||||
|
||||
/** Gets an item from the JS. */
|
||||
std::string CC_DLL localStorageGetItem( const std::string& key );
|
||||
bool CC_DLL localStorageGetItem( const std::string& key, std::string *outItem );
|
||||
|
||||
/** Removes an item from the JS. */
|
||||
void CC_DLL localStorageRemoveItem( const std::string& key );
|
||||
|
|
Loading…
Reference in New Issue