ios - 为什么需要为每个线程创建 ABAddressbookRef?

标签 ios thread-safety abaddressbook

苹果说:

Important: Instances of ABAddressBookRef can not be used by multiple threads. Each thread must make its own instance.

但为什么呢?

我知道某些特定的类或操作必须在主线程中完成。

而且我知道有些对象不是线程安全的(这意味着如果这些对象被两个不同的线程同时访问会导致问题)。

但是,如果您可以确保线程不安全的对象在任何时刻都只被一个线程访问,那么应该没有问题。

到目前为止我理解正确吗?

我不明白的是,为什么需要为每个线程创建像 ABAddressbookRef 这样的对象?为什么苹果会说这样的话?如果只是线程不安全,Apple可以说是线程不安全,处理时要小心。但是为什么需要为每个线程创建一个呢?有什么我不知道的原因吗?

ABAddressbookRef 的实现是否依赖于创建它的线程?

PS:我记得 Core Data 还说需要为每个使用它的线程创建 ManagedObjectContext。

最佳答案

为了结束猜测,我使用付费支持向 Apple 询问有关 ABAddressBookRef 和多线程的明确答案。

这是我问的:

There has been a lot of speculation on the matter and I decided I would like to ask for a definite answer from an engineer who is well aware of the implementation details of ABAddressBook Framework.

The documentation states: Important Instances of ABAddressBookRef cannot be used by multiple threads. Each thread must make its own instance.

If I take this literally, it means that ABAddressBookRef must be created in each block even with GCD serial queues since GCD does not give any guarantees about threads beside the global main thread.

I want to ask if this is literally how it is meant, OR, is it enough to ensure that no two threads are accessing the same ABAddressBookRef at the same time at any moment which GCD private serial queue does guarantee.

这是我从 Apple 得到的返回。

Thank you for contacting Apple Worldwide Developer Technical Support. I am responding to let you know that I have received your request for technical assistance. That is correct. This is because an address book object should never cross thread boundaries. As such, each block must have its own instance.

这是个坏消息。

关于ios - 为什么需要为每个线程创建 ABAddressbookRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7013411/

相关文章:

ios - NSDictionary - 需要获取键子元素的值

java - 如何杀死有一段时间(真)的线程?

c++ - 从可能由不同线程修改的位集 (C++) 访问(读取)位是否安全

objective-c - iOS 中未调用 ABAddressBook 外部回调

ios - SceneKit 节点跟随相机

ios - Trigger.io Facebook "Login Failed, EXPECTED_FAILURE"

java - 以下方法是线程安全的

ios - 如何在 iOS 6 中从 AddressBook 中获取联系人并将其存储在数组中?

ios - ABAddressBookCopyArrayOfAllPeople 不返回任何人

ios - 如何在 swift 3 tableview 中过滤多维数组