tags:

views:

21

answers:

1

Hey,
I'm saving a copy of all contacts in a database.
Upon launching, I want to check if there is a new contact in the address book in order to add it to my data base.

How to perform this check? I can't find the suitable logic to implement it.

+2  A: 

There's not an easy way to get "new contacts" in the address book. Instead you'll have to do something like this:

  1. Get an array of all the contacts in the address book.
  2. Loop through the array and see if the contact is in your database. (NOTE: Record ids can change!)
  3. If you find a contact that is not in your database, add it.

It's probably also smart to store the last modification date of the property and compare that with each record so you can know if something has changed on an existing record.

Finally, this could take a while depending on the number of contacts the user has. It's probably something you want to do without blocking the UI. (Another note: Address Book types are not thread safe. Keep this in mind if you're working in a background thread.)

While your app is running, you can register address book change notification callback to be alerted of changes that happen while your app is running.

Robot K
So basically I have to launch a thread and put this logic inside it? What is the best place (method) to launch this thread? my view controller's viewDidLoad method? Thank you for this precious help.
El Gusto
It's hard to say without knowing more about your app, typically this sort of thing is kicked off by your app delegate once it's done launching. It's not quite what you're doing, but Apple's SeismicXML demo app (https://developer.apple.com/library/ios/#samplecode/SeismicXML/Introduction/Intro.html) does work in a separate thread and notifies the main thread when changes are found. It would be a good model to follow.
Robot K