Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a scene async so you can have a loading screen?

Tags:

sprite-kit

My scene loads can take a while, and I want to be able to show a loading animation, however, everything locks up. Is there a way to load the next scene async and get a callback when its ready?

like image 402
AwDogsGo2Heaven Avatar asked Mar 21 '23 15:03

AwDogsGo2Heaven


1 Answers

You can schedule a block for concurrent execution using dispatch_async. Load scene in async block then perform callback method on the main thread like this:

__weak MyClass *weakself = self; 
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
    //Background thread
    //Load scene here
    dispatch_async(dispatch_get_main_queue(), ^(void){
        //Main thread
        //Call your callback method here
        [weakself sceneLoaded:loadedScene];
    });
});
like image 102
Dobroćudni Tapir Avatar answered Apr 08 '23 08:04

Dobroćudni Tapir