How to use thread to processing long logic?

How to use thread to processing long logic?
0.0 0

#1

Hi All,

I have a long processing logic and i want to show user a loading indicator it is processing in other thread.
Any ideas,
Thanks


#2

Unless things have changed, I believe JavaScript is single-threaded, so there are only 2 options that come to mind:

  1. If your app has C++ code, consider writing your logic in C++ and executing it in a background thread. This might not work if you are using CCObject-derived objects. Or,
  2. If you can, break up your logic into smaller parts. Then, between each part, update your loading indicator and wait until the next frame before continuing the logic. Doing this will allow the engine to update what is being shown on the screen. I did this for a loading screen. I needed to load a lot of textures, so I would load 1, then update my progress bar, wait a frame and then load the next texture.

#3

Thanks you for the reply.
I would like to give more about my problem. I store my pretty big data in the localstorage. And i want to show a user the loading indicator while this data is being query.

  • Your solution 1: Is it possible to access the localStorage from the C++, Objective-c or Java? (if possible, would you mind point me the sample code?)

  • Solution 2: Would u mind point me the sample code?


#4

There does appear to be code related to LocalStorage in C++, but I have not used it so I can’t say for certain or how to use it.

Not sure what exactly you are doing with your data, so I’m not sure how best to provide sample code (and I haven’t written JS code in a while), but it would be something like:

nextWork:function() {
    // do your next work with your data
    
    // update your loading indicator
    this.updateLoadingIndicator();

    // wait until the next frame to do more work to give the app
    // time to refresh the screen
    this.scheduleOnce(this.nextWork);
}

You just need to keep track of where you are at with your data so nextWork knows what to do next.