Skip to main content

Offline Functionality

The ContactsManager SDK stores contact data locally on your device, enabling basic offline functionality. This guide explains how contacts are managed in offline scenarios and how synchronization works.

Offline-First Approach

The SDK stores all contact data locally on the device using SwiftData, which means:
  1. All contact data is accessible offline through the local database
  2. Contacts can be viewed and queried even without an internet connection
  3. Changes made while offline are synchronized when connectivity is restored

Working with Contacts Offline

Fetching Contacts

You can fetch contacts from the local database at any time, regardless of connection status:
You can also fetch contacts with specific field types:

Fetching a Single Contact

To retrieve a specific contact:

Synchronization Management

Manual Synchronization

The SDK automatically attempts to synchronize contacts when the app becomes active, when the contact store changes, or when contacts access is granted. You can also trigger manual synchronization:

Background Synchronization

You can enable background synchronization by calling the following method during app initialization (typically in your AppDelegate or App):
This registers a background task with the system that will periodically synchronize contacts even when your app is in the background.

Social Features in Offline Mode

Social features rely more heavily on server connectivity, but some operations are queued for later synchronization when offline.

Following Contacts

To follow a contact (will be synchronized when online):

Offline Limitations for Social Features

While contact data is fully accessible offline, social features have some limitations:
  1. New user discovery: Finding new users requires connectivity
  2. Activity feeds: New remote events can’t be fetched while offline
  3. Follow status: New follow relationships from other users won’t be visible until synchronization

Implementing an Offline-Aware UI

To provide a good user experience, your UI should be aware of potential synchronization states. Here’s a simplified example:

Troubleshooting Offline Issues

Common Issues

  1. Synchronization not completing
    • Check for connectivity issues
    • Ensure the SDK is properly initialized
    • Verify that authentication tokens are valid
  2. Unexpected data loss
    • Ensure the app isn’t forcefully terminated during sync
    • Verify that device storage isn’t critically low
    • Check for permission changes while offline
  3. Background sync issues
    • Verify that you’ve called enableBackgroundSync() during app initialization
    • Check that your app has the proper background modes enabled in capabilities
    • Ensure you have the “Background Processing” entitlement for your app

Logging and Debugging

The ContactsManager SDK uses the OSLog system for logging important events. To view these logs, use the Console app on macOS with your device connected, and filter for the subsystem “com.contactsmanager”.