Question

I am creating a game with Sprite Kit, and all of my game code runs through the update: method of the SKScene that is running. I do not create any other threads myself.

It is my understanding that the application delegate methods:

- (void)applicationWillResignActive:(UIApplication *)application
- (void)applicationDidEnterBackground:(UIApplication *)application
- (void)applicationWillEnterForeground:(UIApplication *)application
- (void)applicationDidBecomeActive:(UIApplication *)application

Run on the main thread.

If update: is called on the main thread, can I be certain that if I make changes to member variables of the running SKScene (when any of the delegate methods is called), that the update: method will see the changes ?

Is it possible that any of the above delegate methods will execute while the update: method is executing and vice-versa?

EDIT: Do the delegate methods and [SKScene update:] execute on the same (main) thread ?

Was it helpful?

Solution 2

Ok. I'm answering my own question because I've verified my answer

1) The delegate methods and [SKScene update:] are executed on the main thread. I verified this by calling [NSThread isMainThread], and also because Xcode displays the thread name in the thread execution / Call stack window

2) While the app is in inactive state, update is still called, so it isn't so important whether it is called in the next loop or same loop as when applicationWillResignActive: is called.

3) Once applicationDidEnterBackground: gets executed, update: is not called.

OTHER TIPS

I'm not a sprite expert but in general: Two methods will never run at the same time in the same thread.

While your [update:] method is executing, it is blocking that thread for other executions (check the NSRunLoop mechanism). So if a delegate methods need execution at that time in that thread, it will be scheduled for a next run loop cycle. The same counts vice versa.

There's some nice explanation here: Understanding NSRunLoop with some reference to the Apple docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top