Why do UI elements only work on the main thread?

I am trying modeling run loop in a simple project. Could you post the source code of RunLoop?

Answered by Frameworks Engineer in 717394022

If you are talking about CFRunLoop, that is part of CoreFoundation and open source: https://opensource.apple.com/source/CF/CF-635/CFRunLoop.c.auto.html

UIKit is a main thread only API unless explicitly stated otherwise because most parts in UIKit would need to be synchronized explicitly with each other and that would cause a lot of locking, which in the end would be a bigger hit on performance than having everything on the main thread. The biggest part here probably is UIKit's view graph. The whole view hierarchy is a graph that needs to be kept in sync. So every time a layout pass happens or a view is added or removed from that graph, that would need to be synchronized with all participating threads.

Accepted Answer

If you are talking about CFRunLoop, that is part of CoreFoundation and open source: https://opensource.apple.com/source/CF/CF-635/CFRunLoop.c.auto.html

UIKit is a main thread only API unless explicitly stated otherwise because most parts in UIKit would need to be synchronized explicitly with each other and that would cause a lot of locking, which in the end would be a bigger hit on performance than having everything on the main thread. The biggest part here probably is UIKit's view graph. The whole view hierarchy is a graph that needs to be kept in sync. So every time a layout pass happens or a view is added or removed from that graph, that would need to be synchronized with all participating threads.

Why do UI elements only work on the main thread?
 
 
Q