Cocos2dx v2.2.6 CCUserDefault Problem


I followed @yinjimmy example to create a v2.2.6 project for Android Studio.

Now I have a problem with CCUserDefault not working properly. If I call

cocos2d::CCUserDefault::sharedUserDefault()->setIntegerForKey(string, num);

and pass correct values for the string and int, the program crashes. I believe this is because the C++ code is calling back to the Java code on the wrong thread. So when this line:

return setIntegerForKeyJNI(pKey, value);

is called, the app will stop running.

Is there a way to modify the CCUserDefaultAndroid.cpp file so that it calls setIntegerForKeyJNI(pKey, value); on the other thread, and not on the cocos thread.

Thanks for your help

Or you can perform the function call on cocos thread.

7. Add void CCScheduler::performFunctionInCocosThread(const std::function<void ()> &function)


CCLog("> Current cocos thread = %lu", std::hash<std::thread::id>{}(std::this_thread::get_id()));
std::thread th ([pDirector]() {
    CCLog("> Hello from thread = %lu", std::hash<std::thread::id>{}(std::this_thread::get_id()));
    pDirector->getScheduler()->performFunctionInCocosThread([]() {
        CCLog("> Hello from cocos thread = %lu", std::hash<std::thread::id>{}(std::this_thread::get_id()));