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

时光怂恿深爱的人放手 提交于 2019-12-05 04:18:23

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!