Managing the Background State

To allow your application to continue to safely run when it goes into the background, you must implement code in its AppDelegate class to ensure that the SUPApplication instance's connection to the server shuts down gracefully when going into the background, and starts up when the application becomes active again.

This is important because in iOS, when an application goes into the background, it can have its network sockets invalidated, or the application may be shut down at any time. For correct behavior of the SUPApplication connection, the connection needs to be stopped when in background, and only started again when the application goes back to the foreground.

In addition, if your application is using replication based synchronization, and is synchronizing a large amount of data at the time the application goes into background, it may be necessary to interrupt the sync. To do this, the synchronization needs to be done using a sync status listener, and the applicationDidEnterBackground method must notify the listener to set the info.state flag to SYNC_STATUS_CANCEL (see Developer Guide: iOS Object API Applications > Client Object API Usage > Callback and Listener APIs > SyncStatusListener API for more details).

You must implement two appDelegate methods: applicationDidEnterBackground and applicationWillEnterForeground.

Note: The applicationWillEnterForeground method is also called when the application first starts up, where most applications would have code already to register the application and start the SUPApplication connection. This example code uses a boolean wasPreviouslyInBackground so that the applicationWillEnterForeground method can detect whether it is called on coming out of the background or is called on a first startup.
BOOL wasPreviouslyInBackground = NO;

- (void)applicationDidEnterBackground:(UIApplication *)application
{
  /*
   Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
   If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
   */
     @try
     {
       wasPreviouslyInBackground = YES;
       [SUP101SUP101DB disableSync];
       [SUPApplication stopConnection:0]; 
     }
     @
     catch (NSException *ee)
     {
       // log an error or alert user via notification
     }
} 



- (void)applicationWillEnterForeground:(UIApplication *)application
{
  /*
   Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
   */  
if(wasPreviouslyInBackground)
 // Run these in the background since these are blocking calls and 
 // this will be called from the UI thread.
 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
 dispatch_async(queue, ^
   {
     @try
     {
       [SUP101SUP101DB enableSync];
       [SUPApplication startConnection:30];
     }
     @
     catch (NSException *ee)
     {
       // log an error or alert user via notification
     }
   });
}