Contacts sync in small batches whenever the app is in the foreground. If Background Sync is enabled, contacts sync even when the app is not active. Even large contact books with 1000+ contacts sync in 60-90 seconds, depending on network conditions.For subsequent app launches, only unsynced or modified contacts are synchronized, ensuring minimal network and battery usage.The SDK automatically triggers sync when:
The app becomes active
Contact store changes are detected
Contact access status changes from denied to granted
You can track the progress and results of contact syncing operations using platform-specific callbacks.
Copy
import ContactsManager// Set up a completion handler to receive sync metricsContactsService.shared.onContactsSynced = { metrics in print("Sync completed with the following metrics:") print("Total device contacts: \(metrics.totalDeviceContacts)") print("New contacts added: \(metrics.newContacts)") print("Updated contacts: \(metrics.updatedContacts)") print("Unchanged contacts: \(metrics.unchangedContacts)") print("Deleted contacts: \(metrics.deletedContacts)") print("Total processed: \(metrics.totalProcessed)") // Update your UI or notify the user about sync completion DispatchQueue.main.async { // Update UI elements self.updateSyncStatusLabel(metrics: metrics) }}// Example function to update UIfunc updateSyncStatusLabel(metrics: SyncMetrics) { let totalChanged = metrics.newContacts + metrics.updatedContacts + metrics.deletedContacts if totalChanged > 0 { syncStatusLabel.text = "Synced \(totalChanged) contact changes" } else { syncStatusLabel.text = "Contacts up to date" }}
The SDK provides detailed metrics through the SyncMetrics structure:
Copy
public struct SyncMetrics { /// Total number of contacts found on device public let totalDeviceContacts: Int /// Number of new contacts added public let newContacts: Int /// Number of contacts that were updated public let updatedContacts: Int /// Number of contacts that were unchanged public let unchangedContacts: Int /// Number of contacts that were deleted public let deletedContacts: Int /// Total number of contacts processed public let totalProcessed: Int}
These metrics help you understand the scope and impact of each sync operation.
Sometimes you may need to force a complete resync of all contacts, regardless of their current sync status. This is useful when troubleshooting sync issues or ensuring data consistency after server-side changes.
import ContactsManager// Force resync all contactsdo { let resyncedCount = try await ContactsService.shared.forceResyncAllContacts() print("Successfully resynced \(resyncedCount) contacts")} catch { print("Force resync failed: \(error.localizedDescription)")}// You can also use the completion handler to track progressContactsService.shared.onContactsSynced = { metrics in print("Force resync completed:") print("Total contacts processed: \(metrics.totalProcessed)") // All contacts will appear as "updated" since sync timestamps were reset}
When users log out of your application, you should properly clean up the ContactsManager service to ensure user data privacy and prepare for the next user session.
import ContactsManager// Logout: Clean up ContactsManager when user logs outdo { // This clears all local contact data and user session try await ContactsService.shared.reset() print("User logged out, ContactsManager cleaned up successfully")} catch { print("Logout cleanup failed: \(error.localizedDescription)")}// When a new user logs in, reinitialize with their credentialsdo { try await ContactsService.shared.initialize( withToken: "new-user-token", options: ContactsManagerOptions( verboseLogging: false, autoSyncEnabled: true ) ) print("ContactsManager initialized for new user")} catch { print("Initialization failed: \(error.localizedDescription)")}