Performing Background Synchronization

Set up the iOS application to perform a background synchronization.

  1. Enable this feature in the Xcode project for the application:
    1. Select the Capabilities tab on the project settings screen.
    2. Expand Background Modes and select the Background Fetch mode.
      This adds the requisite entry to the info plist file for your application.
  2. Set the minimum fetch interval in the application’s application:didFinishLaunchingWithOptions: delegate method.
    Typically, this is set to UIApplicationBackgroundFetchIntervalMinimum to fetch as often as allowed.
     (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
       [[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum]; 
    . 
    . 
    .
    The default value is UIApplicationBackgroundFetchIntervalNever, so if it is not set to a new value upon launch, the application never wakes up to perform background fetches.
  3. Implement the method.
    - ( void )application:( UIApplication  *)application performFetchWithCompletionHandler:( void 
     (^)( UIBackgroundFetchResult ))completionHandler 
    .

    This is the method that gets called when the application restarts in the background by the system to perform a fetch.

    In Sybase Unwired Platform, to synchronize in the background, this method must execute as follows:

    1. Set the application ID. This is the only SUPApplication API that should be called.
    2. Set up the connection profile with the correct encryption key to make the client DB accessible.
    3. Set up the synchronization profile with the correct parameters (server name, port, protocol, network stream parameters, and the Sybase Unwired Platform username and password).
    4. Synchronize.

Background Synchronization Example Code

- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    UIBackgroundFetchResult result = UIBackgroundFetchResultNoData;
    
    NSLog(@"background fetch called");
    SUPApplication* app = [SUPApplication getInstance];
    if (!SUPApplication.applicationIdentifier)
    {
        app.applicationIdentifier = @"supsample";
    }
    
    if(!self.firstRun)
    {
        SUPConnectionProfile *cp = [SUPSampleSUPSampleDB getConnectionProfile];
        [cp setEncryptionKey:@"e77d4007dd40d75037e3bdba005f38dc1002e41043e7ad7c17cd34eb5eb8d7bf75e01df5001db8085179f05e040c307c"];
        [SUPSampleSUPSampleDB closeConnection];
        SUPConnectionProfile *sp = [SUPSampleSUPSampleDB getSynchronizationProfile];
        [sp setAsyncReplay:NO];
        [sp setUser:self.userName];
        [sp setPassword:@"s3pAdmin"];
        [sp setServerName:self.serverName];
        [sp setPortNumber:[self.serverPort intValue]];
        [sp setNetworkProtocol:@"http"];
        [sp setNetworkStreamParams:@"url_suffix=/ias_relay_server/client/rs_client.dll/douglowder.dddRBS"];
        [sp setDomainName:@"default"];
        
        @try
        {
            [SUPSampleSUPSampleDB synchronizeWithListener:[CallbackHandler getInstance]];
            result = UIBackgroundFetchResultNewData;
            NSLog(@"Background fetch succeeded");
        }
        @catch (NSException *exception)
        {
            result = UIBackgroundFetchResultFailed;
            NSLog(@"Background fetch failed: %@",[exception description]);
            NSLog(@"%@",[exception callStackSymbols]);
        }
    }
    completionHandler(result);
}