1

I try long running background task by starting timer repeatedly.But Background task run properly for first only, next time onwards it calls endBackgroundTask method randomly and continuously.

My Background Task code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if([self isMultiTaskingSupported])
    {
        NSLog(@"Multitasking Supported");
    }
    else
    {
         NSLog(@"Multitasking Not Supported.");
    }

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[INNOViewController alloc] initWithNibName:@"INNOViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (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.

    [self startBackgroundTask:application];
}

-(void) startBackgroundTask:(UIApplication *)application
{
    if([self isMultiTaskingSupported]==NO)
    {
        return ;
    }

    self.myTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerMethod:) userInfo:nil repeats:YES];

    self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void){
        [self endBackgroundTask:application];

        [self startBackgroundTask:application];
    }];
}

- (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 (self.backgroundTaskIdentifier != UIBackgroundTaskInvalid) {
        [self endBackgroundTask:(UIApplication *)application];
    }
}

-(BOOL) isMultiTaskingSupported
{
    BOOL result = NO;

    //NSLog(@"before %hhd",result);
    //NSLog(@"method exist or not -> %hhd",[[UIDevice currentDevice] respondsToSelector:@selector(isMultiTaskingSupported)]);
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
    {
        NSLog(@"isMultiTaskingSupported method available in sdk");
        result=[[UIDevice currentDevice] isMultitaskingSupported];
        //NSLog(@"after %hhd",result);
    }
    return result;
}


-(void) timerMethod:(NSTimer *)paramSender
{
    NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];

    if(backgroundTimeRemaining == DBL_MAX)
    {
        NSLog(@"Background Time = undetermined");
    }
    else
    {
         NSLog(@"Background Time = %0.02f",backgroundTimeRemaining);
    }
}

-(void) endBackgroundTask:(UIApplication *)application
{
    NSLog(@"endBackgroundTask");
    dispatch_queue_t mainQueue =  dispatch_get_main_queue();

    __weak INNOAppDelegate *weakSelf=self;

    dispatch_async(mainQueue, ^(void){
        //Complete a long background task in AppDelegate
        INNOAppDelegate *strongSelf = weakSelf;

        if(strongSelf != nil)
        {
            [strongSelf.myTimer invalidate];
            [[UIApplication sharedApplication]endBackgroundTask:self.backgroundTaskIdentifier];
            strongSelf.backgroundTaskIdentifier=UIBackgroundTaskInvalid;

            NSLog(@"cleaned BackgroundTask");


        }
    });


}

Log:

2013-04-24 08:50:14.510 MultiTasking[651:c07] Background Time = 17.00
2013-04-24 08:50:15.510 MultiTasking[651:c07] Background Time = 16.00
2013-04-24 08:50:16.510 MultiTasking[651:c07] Background Time = 15.00
2013-04-24 08:50:17.510 MultiTasking[651:c07] Background Time = 14.00
2013-04-24 08:50:18.510 MultiTasking[651:c07] Background Time = 13.00
2013-04-24 08:50:19.510 MultiTasking[651:c07] Background Time = 12.00
2013-04-24 08:50:20.510 MultiTasking[651:c07] Background Time = 11.00
2013-04-24 08:50:21.510 MultiTasking[651:c07] Background Time = 10.00
2013-04-24 08:50:22.510 MultiTasking[651:c07] Background Time = 9.00
2013-04-24 08:50:23.510 MultiTasking[651:c07] Background Time = 8.00
2013-04-24 08:50:24.510 MultiTasking[651:c07] Background Time = 7.00
2013-04-24 08:50:25.510 MultiTasking[651:c07] Background Time = 6.00
2013-04-24 08:50:26.510 MultiTasking[651:c07] Background Time = 5.00
2013-04-24 08:50:26.510 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.511 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.512 MultiTasking[651:c07] cleaned BackgroundTask
2013-04-24 08:50:26.512 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.512 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.513 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.514 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.514 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.515 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.515 MultiTasking[651:c07] cleaned BackgroundTask
2013-04-24 08:50:26.516 MultiTasking[651:c07] cleaned BackgroundTask
2013-04-24 08:50:26.516 MultiTasking[651:c07] cleaned BackgroundTask
2013-04-24 08:50:26.516 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.517 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.518 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.518 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.519 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.519 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.520 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.520 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.521 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.522 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.523 MultiTasking[651:c07] endBackgroundTask
2013-04-24 08:50:26.523 MultiTasking[651:c07] isMultiTaskingSupported method available in sdk
2013-04-24 08:50:26.524 MultiTasking[651:c07] endBackgroundTask
1
  • Did you end up getting this working, or was it not possible? Nov 14, 2013 at 10:38

1 Answer 1

1

It might be due to your expiration handler block ending the task and then starting it again.

In addition, you still capture self in your endBackgroundTask block (use strongSelf.backgroundTaskIdentifier)

4
  • yes i need to start background task again after first background task finished
    – Ramprasad
    Apr 24, 2013 at 11:38
  • You should not start another task when your expiration handler is called. you should stop your background execution (cleanup). read here.
    – Dan Shelly
    Apr 24, 2013 at 12:25
  • I need to run background task continuously
    – Ramprasad
    Apr 24, 2013 at 12:29
  • You can't, Apple does not allow endless BG execution (unless you register as audio player or navigational application [AFAIK]). see here
    – Dan Shelly
    Apr 24, 2013 at 12:33

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.