How to re-trigger a process repeatedly in background in xcode

155 Views Asked by At

I'am working on an application in xcode-5, I need to upload some data in background mode, I need a set of code to be repeated till the device terminates the background process. I am able to do it once but I need to repeat again and again so that i can check the connectivity and perform the upload. I doing the same thing when the application comes in active mode. Currently I'm using Timers with the following code :

- (void)applicationDidEnterBackground:(UIApplication *)application
{
[bgrdTimer_ActiveMode invalidate];
bgrdTimer_ActiveMode=nil;

    self.backgroundTask=[application beginBackgroundTaskWithExpirationHandler:^{
        self.backgroundTask=UIBackgroundTaskInvalid;
    }];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{

    bgrdTimer_BackGroundMode=[NSTimer scheduledTimerWithTimeInterval:40.00 target:self selector:@selector(repeatUploadProcess_BackGroundMode) userInfo:nil repeats:YES];

    });
 }


 - (void)applicationDidBecomeActive:(UIApplication *)application
 {
   [bgrdTimer_BackGroundMode invalidate];
   bgrdTimer_BackGroundMode=nil;
   bgrdTimer_ActiveMode=[NSTimer scheduledTimerWithTimeInterval:40.00 target:self selector:@selector(repeatUploadProcess_ActiveMode) userInfo:nil repeats:YES];
 }

repeatUploadProcess_ActiveMode and repeatUploadProcess_BackGroundMode are the methods containing the set of code which is suppose to be repeated

the problem is when I invalidate the bgrdTimer_ActiveMode the other timer dosn't gets invoked.

1

There are 1 best solutions below

1
On

Have you enabled the background fetch mode? Click on the project target, and under the capabilities tab select "background fetch".

Theoretically you should also be setting [application endBackgroundTask:bgTask]. It sounds like you know, but if it never ends, iOS will end up killing your app.