什么时候需要担心iOS应用程序中的线程安全性?

问题描述:

在编写通常的视图控制器代码时,我是否可以假定仅从单个事件循环线程中调用它?我需要什么样的类来使线程安全?涉及多个线程的通常情况是什么?

When writing the usual view controller code, can I assume that this will only be called from a single event-loop thread? What kind of classes do I need to make thread-safe? What are the usual situations where multiple threads are involved?

如果您正在编写普通的UIViewController代码,则无需担心iOS中的线程安全性.在iOS中,有关UI的任何消息都应在主线程上运行.

If you are writing normal UIViewController code, you don't need to worry about thread-safety in iOS. In iOS, any message about UI should be running on the main thread.

如果您不自行在后台执行某些消息,通常不必担心线程,在大多数情况下,它将始终位于主线程上.

If you don't perform some message in background by you self, normally, you don't have to worry about thread, in most situations, it will always be on the main thread.

P.S.诸如Game Kit之类的某些框架有时会在后台执行消息,但这与UI无关,Apple的文档将警告您确保消息是否在主线程上运行.

P.S. Some Frameworks like Game Kit will some times perform messages in background, but it's not about UI and the document from Apple will warn you to make sure if the message is running on main thread.