Fix local storage get item issue when item inexist

This commit is contained in:
pandamicro 2015-04-23 19:06:06 +08:00
parent 3409f0fcd4
commit 6b4f8cc0ab
3 changed files with 20 additions and 13 deletions

View File

@ -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 */

View File

@ -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 */

View File

@ -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 );