ABAddressBook 框架可以与 Grand Central Dispatch 的串行队列一起使用吗?(线程问题)
在苹果的文档中,苹果表示:
重要提示:ABAddressBookRef 的实例不能被多个使用 线程。每个线程必须创建自己的实例。
和
记录对象不能安全地跨线程传递。相反,您 应传递相应的记录标识符
第一个是什么意思?如果我可以保证每一刻只有一个线程正在访问ABAddressBookRef,我可以在多个线程中使用这个ABAddressBookRef吗?
第二个,不能安全地跨线程传递到底是什么意思?
并且似乎 GCD 不保证所有块都在同一线程中执行,即使这些块位于同一串行队列中。
那么这是否意味着我不能使用GCD来处理ABAddressBook框架? 或者我只能在每个块中创建一个ABAddressBookRef?(我已经测试过这个,真的很慢)
In apple's document, apple says:
Important: Instances of ABAddressBookRef can not be used by multiple
threads. Each thread must make its own instance.
And
Record objects can not be passed across threads safely.Instead,you
should pass the corresponding record identifier
What does the first one mean?If i can assure that every moment there is only one thread that is accessing the ABAddressBookRef, can I use this ABAddressBookRef in multiple threads?
And the second one,what does can not be passed across threads safely really mean?
And seems that GCD do not assure all the blocks are excuted in the same thread even the blocks are in a same serial queue.
So does this mean that I can't use GCD to deal with the ABAddressBook framework?
Or I can only create a ABAddressBookRef in each block?(I've tested this, really slow)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题涵盖了同样的问题。这并不是说 ABAddressBook 不是线程安全的,而是意味着您根本无法使用多线程。
但是,您可以编写一个带有 getter / setter 方法的包装类来执行您需要的操作,这些方法可以从调度块内的任意线程调用。不过,包装器内的所有“变异”函数都必须发生在同一个线程上。也许考虑看看 dispatch_once< /a> 使用 grand-central 防止多重实例化的文档。
This question covers the same issue. It's not that ABAddressBook isn't thread-safe, it means you can't use multi-threading at all.
However you could write a wrapper class with getter / setter methods to do what you need, which could be called from arbitrary threads inside a dispatch block. All the 'mutating' functions inside the wrapper will all have to happen on the same thread though. Maybe consider looking at the dispatch_once documentation for using grand-central to protect against multiple instantiation.