In SpriteKit does touchesBegan run in the same thread as the SKScene update method?
Asked Answered
K

1

7

In the Apple documentation here Advanced Scene Processing it describes the update method and how a scene is rendered, but it does not mention when input is processed. It is not clear if this is in in the same thread as the rendering loop, or whether it is concurrent with it.

If I have an object that I update from both the SKScene update method and the touchesBegan method (in this case of a SKSpriteNode) do I have to worry about synchronising the two accesses to my object?

Kassandra answered 20/4, 2015 at 18:14 Comment(2)
I'm not completely sure, but I am 99% sure it will be in the main thread since touch events are received by the main run loop.Xl
I think we can make that 99.9% now. Thanks for the comment - it was good enough to work with.Kassandra
K
4

So after a few days with no answer I set up some experiments. By the way, these tests are run on the simulator and not on an actual device, but I think it would be the same.

First test, I set a breakpoint in the debugger on touchesBegan and looked at the stack trace. It appears that touchesBegan is called from the first thread and from the main loop - the same place as the rest of the logic, so this looks good for a singe-threaded approach.

Second test, I overrode the various methods in the scene mentioned in the Advanced Scene Processing link above and added print statements to show th name of each function called. Then I added a print statement to the touchesBegan method.

On running the app, the output was:

update
didEvaluateActions
didSimulatePhysics
didApplyConstraints
didFinishUpdate
touchesBegan in scene
update
didEvaluateActions
didSimulatePhysics
didApplyConstraints
didFinishUpdate
update

and this pattern was repeated whenever I clicked.

No amount of clicking gave me anything else than touchesBegan being called between the didFinishUpdate (that is, the end of one cycle) and the update (the beginning of the next).

Conclusion: touches processing happens in the main loop before the update method is called. It is therefore not necessary to synchronise resources between the two methods.

Kassandra answered 23/4, 2015 at 18:31 Comment(2)
The doc refer to the order --> developer.apple.com/library/prerelease/ios/documentation/…Phenomenalism
Yes, but it doesn't mention where the touches are processed, which was my question.Kassandra

© 2022 - 2024 — McMap. All rights reserved.