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

前端之家收集整理的这篇文章主要介绍了ios – 为什么需要为每个线程创建ABAddressbookRef?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
苹果说:

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

但为什么?

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

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

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

到目前为止我明白了吗?

我不能理解的是,为什么要为每个线程创建一些像ABAddressbookRef的对象?为什么苹果会这样说?如果只是它是线程不安全的,苹果可以说它是线程不安全的,处理它时要小心.但是为什么每个线程都需要创建一个?有什么理由我不知道吗?

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.

这就是苹果的回报.

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相关文章