1

ABAddressBookRef只能被一个线程访问。我认为一个好的模型会ABAddressBookRef在后台有一个线程,除了主线程。

如何确保使用时只有一个线程NSOperationQueue?简单地将最大并发设置为 1 并不能保证它在同一个线程上运行。

我应该使用其他独特的线程,如网络线程吗?

4

1 回答 1

1

You can manually create a thread and redirect all address book access to it.

You create a thread with something like this (adapted from documentation):

NSThread* myThread = [[NSThread alloc] initWithTarget:[MyThread new]
                                    selector:@selector(myThreadMainMethod)
                                    object:nil];
[myThread start];  // Actually create the thread

Note that for the thread to be useful, you have to implement a run loop in thread's main method.

See example implementation of run loop in this answer.

You are then able to do stuff on this thread using the NSObject's method performSelector:onThread:withObject:waitUntilDone:.

Here's a wrapper library for ABAddressBookRef that implements this concept – RHAddressBook.

于 2013-11-26T23:03:23.220 回答