1

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 ?

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190

2 Answers2

3

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.

Community
  • 1
  • 1
Leijonien
  • 1,415
  • 14
  • 16
  • Are you saying that the delegate methods, and the [SKScene update:] method in sprite kit both execute on the main thread? – Rahul Iyer Nov 16 '13 at 15:08
  • Yes, all UI updates are done in the main thread. You can easily verify that for yourself by putting `NSLog()` statements in the methods, as NSLog will automatically indicate the thread in each log. – Leijonien Nov 16 '13 at 15:26
  • Ok. I see what you're saying. I still don't understand one thing though: If both the delegate methods and the update: method are both running on the main thread, does it guarantee that both methods will run? Or if the delegate methods are called, does it mean none of the other methods that normally run on the run loop will get called? – Rahul Iyer Nov 16 '13 at 15:33
  • All these methods will be executed on the main thread, but just never at the same time. So the application delegate methods are never executed while your 'update:' method is still executing. A single thread can do just 1 job (method) at a time, others will wait, as the methods (especially on the main thread) usually just need a short amount of execution time. – Leijonien Nov 16 '13 at 16:17
1

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.

Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190