iosmultithreadingcontactsabrecordref

Multithreaded Contact sync


I am trying to speed up syncing contacts all the contacts on the device to my app. At the moment its blocking the main thread when syncing the contacts.

I added the individual contact setup in an NSInvocationOperation and adding that to a NSOperationQueue.

I know that you can only access the UI in the main thread. What can't you access in regards to Address book data?


Solution

  • The address book framework is generally safe to use from a background thread but their are pitfalls. The important caveats are mentioned in the documentation:

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

    and

    Important Record objects cannot be passed across threads safely. Instead, you should pass the corresponding record identifier. See “Using Record Identifiers” for more information.

    Note that just because NSInvocationOperation are on the same NSOperationQueue does not guarantee that they will be executed on the same thread.