Search code examples
iosdatabasecore-dataaddressbookabaddressbook

Keep reference of ABPerson in CoreData


I'm trying to keep the phone's contacts in my app up to date, in a persistent way. For that I'm thinking of using Core Data.

My plan right now seems highly suboptimal :

  • Browse the address book and every ABPerson in it
  • Store every field in a CoreData persistent store
  • Store the image in a separate file with an unique name and a reference in another "Contact" field.

And I do this every time the app comes in foreground in case the user would change one of his contact's name or picture, etc.

Some of my users have more than 2500 contacts, sometimes the operation lasts up to 10 seconds.

My question is :

Is there a way to keep some kind of reference to my ABPerson in coredata, so I can always load my ABPerson properties everywhere instead of Contact properties? (which would then always be up to date).

And I'm not even sure it's the right decision : Should I always use the ABRecord that I find with a reference? Should I always use my own copied data that I update regularly (from the ABAddressBook)?

If not, do you guys think I'm doing this in a decent way or would you suggest something else?

EDIT:

As asked in the comments:

I need to keep the contacts up to date simply to use their firstname, lastname and picture properties. If I notice the ABRecord changes, I'll update the related custom objects accordingly and that's it. I won't really need anything else afterwards (until they're edited again)

Thanks


Solution

  • Obtain and store only the ABRecord's unique identifier value. This is the one persistent way to reliably refer to the same person repeatedly and consistently.

    You can always get all the other info out of the contacts database by using this unique identifier.

    In iOS, call ABRecordGetRecordID to obtain the person's unique ID. Store that. When you later want to obtain the corresponding person, call ABAddressBookGetPersonWithRecordID.