iTunes fails to sync Contacts

Issue: iTunes failed to update iPad/iPhone with Address Book contacts changed on iMac
Symptoms (at various times):
  • contacts changed on imac didn't update
  • Selected Group in iTunes -> Sync Contacts does not update or remains grey.
Software: iTunes 11.*, IOS 6, iPhone (may occur with other combinations)

Cause: Somehow the internal Address Book database became corrupt.
Identification: This can be seen by opening Console app and looking at the Sync -> syncservices.log. Look through recent history or add an Insert Marker (menu option) and then try to sync. If you see a line that contains 'Error' and a comment about a Record XXXXX where XXXXX is some arbitrary UUID (hex number containing several '-')

Proving the Problem

In my case I had one or two corrupt records. My steps to prove the issue, with iDevice plugging in and iTunes open
1) Backup the address book.
2) Proved the point by deleting all the local contacts (On My Mac) - be careful if you have networked contacts too, ignore them
3) Maybe Optional: unsync/delete all contacts from iPhone. I did this just to ensure a clean starting point.
4) in Address Book (on Mac) create one test group and test contact.
5) The group should pop up within seconds in iTunes under the Contacts group names (if it doesn't try restarting your mac just in case sync services isn't running. There may be ways to start it manually but...)
6) If the previous step works then sync iPhone and check that test contact is synced ok.
7) If ok so far now the next step is finding the broken contacts...

Fixing the Problem

How I did it (not overly efficient):
1) with syncservices.log open (and a marker added), select first half roughly of your (On My Mac) contacts and press delete and confirm. Note make sure you delete them not just remove them from a group.
2) Either sync to iDevice and look for lack of complain OR faster just undelete (fan-Z) and look for issue popping up in log file as record are re-added (this way may not even need iTunes running). If there was no issue repeat for the other half of the file. The error should pop up.
3) Use binary division to narrow the problem down. Note if you have multiple errors you need to track them a little better.

Future Thoughts

There's probably a neat way to turn the UUID into a specific set of cards or other items causing problems. I looked into the Address Book backup files but in my case I didn't find an easy way to identify the broken relationships. I also had a quick look at the database using sqlite3 but didn't spend much time at it. It is probably worth doing this.

Given that the error is in the internal structures it may be that saving all entries as vCards (rather than a Address database archive), then reimporting them all will remove the problem, I didn't think of that at the time and I'm not sure what relationships will be lost (eg groups?).
In the end I had one or two problem records, I simply copied the data, deleted the records and recreated them by hand, maybe exporting/importing would work too.

In researching the problem I also tried:
This actually seemed to make things worse, although in hindsight I think it was just exposing the problem a bit more. I did need to go back into iTunes and re-set all the sync xyz check boxes.


The original document is available at http://dfusion.com.au/wiki/tiki-index.php?page=iTunes+fails+to+sync+Contacts