2

I am using a serial dispatch queue to serialize some network requests when the user moves the app to the background.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    dispatch_queue_t opQ = dispatch_queue_create("com.myapp.network", NULL);
    dispatch_async(opQ, ^{
        [self sendNetworkData1];
        [self sendNetworkData2];
        [self sendNetworkData3];
    });
}

The problem is that when they run on this queue I have created, the app doesn't stay active even for the 5 seconds it is supposed to.

On the contrary, when I send the same requests outside of a queue, they are being sent for approximately 8 sec. but the app crashes afterwards.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
        [self sendNetworkData1];
        [self sendNetworkData2];
        [self sendNetworkData3];
}

I would also like to write the remaining ones on the disk, so that they can be sent the next time the user opens the app.

What's the best way to implement this?

1
  • 1
    Well, can you please edit your question to have better explanation? Also, for the code stuff you said you've wrote, can you please quote actual code of that?
    – TheNavigat
    Aug 27, 2012 at 17:08

1 Answer 1

2

When the application enters the background if it requires additional time to complete some task you will want to notify the OS of that. The detailed documentation is here: http://developer.apple.com/library/ios/#DOCUMENTATION/iPhone/Conceptual/iPhoneOSProgrammingGuide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html. Here's a quick and dirty patch.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
     __block UIBackgroundTaskIdentifier backgroundTask; //Create a task object

    backgroundTask = [application beginBackgroundTaskWithExpirationHandler: ^ {
        [application endBackgroundTask:background_task]; 
        backgroundTask = UIBackgroundTaskInvalid; //Set the task to be invalid
     }];

    dispatch_queue_t opQ = dispatch_queue_create("com.myapp.network", NULL);
    dispatch_async(opQ, ^{
        [self sendNetworkData1];
        [self sendNetworkData2];
        [self sendNetworkData3];
        [application endBackgroundTask:background_task]; 
        backgroundTask = UIBackgroundTaskInvalid; //Set the task to be invalid
    });
}

The bottom line is that you notify that the application needs to run in the background with beginBackgroundTaskWithExpirationHandler: THen when your done you call endBackgroundTask: to notify the OS that you are finished processing in the background. And finally make sure that you reset the backgroundTask variable to UIBackgroundTaskInvalid.

2
  • I need it to be compatible with iOS 3.x though, which I believe this method isn't, right?
    – yannis
    Aug 28, 2012 at 13:53
  • That's right, this won't be compatible with iOS 3.x. I imagine that it is pretty safe at this point to drop support for iOS 3.x.
    – Sean Lynch
    Nov 23, 2012 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.