What is the default assumption if Apple Documentation does not say whether a certain method can or cannot be run on a background thread?

If Apple Documentation does not say whether a certain method is allowed to be run on a background thread other than the main UI thread, does that mean it can or cannot be run on a background thread?

Answered by Frameworks Engineer in 702488022

From the Cocoa documentation about thread safety at https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

Immutable objects are generally thread-safe. Once you create them, you can safely pass these objects to and from threads. On the other hand, mutable objects are generally not thread-safe. To use mutable objects in a threaded application, the application must synchronize appropriately. For more information, see Mutable Versus Immutable.  Many objects deemed “thread-unsafe” are only unsafe to use from multiple threads. Many of these objects can be used from any thread as long as it is only one thread at a time. Objects that are specifically restricted to the main thread of an application are called out as such.

UIKit and AppKit is one of the places where every method is "specifically restricted to the main thread" though unless it is explicitly stated otherwise in the documentation. It's also important to note that a method that does not explicitly state that it can be used form a different thread in those frameworks might work on a background thread today but then break in a future release in very subtle and hard to find bugs.

I would say that if the documentation does not specify a thread, then any thread can be used, as long as UI updates take place on the main thread.

Accepted Answer

From the Cocoa documentation about thread safety at https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

Immutable objects are generally thread-safe. Once you create them, you can safely pass these objects to and from threads. On the other hand, mutable objects are generally not thread-safe. To use mutable objects in a threaded application, the application must synchronize appropriately. For more information, see Mutable Versus Immutable.  Many objects deemed “thread-unsafe” are only unsafe to use from multiple threads. Many of these objects can be used from any thread as long as it is only one thread at a time. Objects that are specifically restricted to the main thread of an application are called out as such.

UIKit and AppKit is one of the places where every method is "specifically restricted to the main thread" though unless it is explicitly stated otherwise in the documentation. It's also important to note that a method that does not explicitly state that it can be used form a different thread in those frameworks might work on a background thread today but then break in a future release in very subtle and hard to find bugs.

What is the default assumption if Apple Documentation does not say whether a certain method can or cannot be run on a background thread?
 
 
Q