AwDogsGo2Heaven
AwDogsGo2Heaven

Reputation: 972

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

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?

Upvotes: 4

Views: 2793

Answers (1)

Dobroćudni Tapir
Dobroćudni Tapir

Reputation: 3102

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];
    });
});

Upvotes: 7

Related Questions