WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Import and check contacts

Forums > Swift

I have an app where I import the contacts from the Contacts app. All that works just fine.

Heres the scenario, if the user taps the button to import contacts into the app and then he/she adds a couple new contacts to their Contacts app. When they reimport, I get duplicate entries. Is there a good way to make sure that the same contact doesn't get imported twice?



I don't know if there is a good way. But it's your responsibility to check if a contact has been already imported. We don't know how you store the imported contacts and if there is an unique identifier for your stored contacts.

All I can say is that there is an identifier for each contact. So basically, you could check for the identifier and make sure it isn't already in your database.



Thank you, I'll add that to my Core Data model and check it before loading new contacts.


Seems like redundant work to write code to exclude duplicate records.

Restricting data sets with unique identifiers is a fundamental function in data storage services, Core Data included.

@twoStraws wrote an article about this.

See --> Core Data Constraints


Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.