Syncing Data

Syncing data from Rover’s servers—such as geofence regions to monitor and notifications to display in the Notification Center—is done through the SyncCoordinator service. You can control when your app performs a sync. The recommended approach is to perform a sync every time your app enters the foreground as well as periodically in the background.


Foreground Syncing

There are two places in your app delegate where you want to perform a sync in order to ensure the most recent data is always fetched when the user opens the app: application(_:didFinishLaunchingWithOptions:) and applicationWillEnterForeground(_:). Implement both of these methods in your app delegate and call the sync() method of the Rover SyncCoordinator.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // ...
    Rover.shared?.resolve(SyncCoordinator.self)?.sync()
    return true
}

func applicationWillEnterForeground(_ application: UIApplication) {
    Rover.shared?.resolve(SyncCoordinator.self)?.sync()
}


Background App Refresh

Background App Refresh lets your app run periodically in the background so that it can update its content. iOS determines when to allow your app to perform a refresh based on network and power conditions. This is an ideal time to sync the latest content from Rover’s servers.

Prepare Your App

To support background fetch you need to add the Background Modes key to your info.plist file. The easiest way to do this is from the Capabilities tab in Xcode. Select your app from the Project Navigator. Then select your app’s target and the Capabilities tab.

Background Modes is the fourth capability in the list. Turn it on and enable Background fetch.

Xcode Capabilities Tab

Set the Minimum Fetch Interval

Next you will need to call the setMinimumBackgroundFetchInterval(_:) method of UIApplication from within your application(_:didFinishLaunchingWithOptions:) method.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // ...
    application.setMinimumBackgroundFetchInterval(UIApplication.backgroundFetchIntervalMinimum)
    return true
}

The default fetch interval for apps is backgroundFetchIntervalNever. Therefore, you must call this method and set a fetch interval before your app is given background execution time.

Perform the Sync

The last step is to implement the application(_:performFetchWithCompletionHandler:) method in your app delegate and tell the Rover SyncCoordinator to perform a sync.

func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    Rover.shared?.resolve(SyncCoordinator.self)?.sync(completionHandler: completionHandler)
}

Remote Notifications

When Rover sends a notification to your app it may include a special flag that tells your app there is data to be fetched. If your app is in the background when a notification arrives, iOS will launch your app and give it time to update its content. Just like Background App Refresh, you should take advantage of this opportunity to sync the latest content from Rover’s servers.

Prepare Your App

Under the same Capabilities tab used to enable Background App Refresh, enable the “Remote notifications” feature.

Remote Notifications

Perform the Sync

implement the application(_:didReceiveRemoteNotification:fetchCompletionHandler:) method in your app delegate and tell the Rover SyncCoordinator to perform a sync.

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    Rover.shared?.resolve(SyncCoordinator.self)?.sync(completionHandler: completionHandler)
}
Required Modules
  • RoverFoundation
  • RoverData