Address Book Thread Safety and Performance

In my understanding of the documentation in the address book and my understanding of the underlying CoreData implementation, it is assumed that the address book should be thread safe, and requests from multiple threads should not cause problems. But I had trouble finding an explicit discussion of thread safety in documents. This raises several questions:

  • Is it possible to use + sharedAddressBook in multiple threads for read-only access? I believe so.
  • To access a record in the background stream, you must use + the address book instead (and save your changes manually). Do I understand this correctly?
  • Has anyone investigated the effect of performance on the simultaneous execution of multiple simultaneous requests in the address book across multiple threads? This should be very similar to executing multiple CoreData queries for multiple threads. I mean, I will gain a little by doing parallel queries, since I assume that they will be serialized when they get into SQLLite, but I'm not sure about that.

I need to make several dozen requests (some complex) against AddressBook and do it in the background thread using NSOperation to avoid blocking the user interface (which it is currently doing). My main question is whether it makes sense to set the maximum parallel operations to a value greater than 1, and whether there is any danger if the application can also write to AddressBook simultaneously in another thread.

+7
cocoa addressbook
source share
1 answer

If the API does not say that it is thread safe, it is not. Even if the current implementation is thread safe, it may not be in the future. In other words, do not use AB from multiple threads.

As an aside, how about being based on CoreData, do you think it will be thread safe? CoreData uses a thread restriction model where only access to the context in one thread is safe, all objects from the context should be available in one thread.

This means that sharedAddressBook will not be thread safe if it supports NSManagedObjectContext for use. It would be safe if AB created a new context every time he needed to do something, and immediately deleted it, or created a context for the thread and always used the appropriate context (possibly keeping a link to it in threadDictionary), Any It would be safe for the case to store something like NSManagedObjects, since contexts will be permanently destroyed, which means that each ABRecord will have to store NSManagedObjectID so that it can recreate the object in the corresponding context whenever it is needed.

It is clear that all this is possible, perhaps this is what has been done, but this is hardly an obvious implementation.

+7
source share

All Articles