12

I'm making a benchmark App for test purposes ONLY. I am not intending this to go to the App Store.

What I need is my NSTimer to continue running on the background using a UIBackgroundTaskIdentifier, save data to a Core Data db and finally push the data to a server (I'm using Parse), after a certain time interval, of course.

So basically, I haven´t found any questions which apply to my specific case. I set my NSTimer like so:

    UIBackgroundTaskIdentifier bgTask;
UIApplication  *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask]; 
}];

self.timer = [NSTimer scheduledTimerWithTimeInterval:self.localInterval target:self selector:@selector(updateCoreData:) userInfo:nil repeats:YES];

the method updateCoreData simply calls the Core Data class and makes the necessary insertions.

I've read about VoIP and the Music playing part, but don't know exactly which one would apply best for my case, nor how to implement them.

7
  • 1
    In your timer handler function you could use Great Central Dispatch to do the lengthy tasks. This way you don't block your event handler from being called.
    – guitarflow
    Feb 9, 2012 at 23:29
  • Are you successful doing this? I have same task and I am trying so hard but yet not successful........Can you help me?
    – Minakshi
    Feb 28, 2012 at 7:12
  • @Xyz Yes, I managed to make it. To make the NSTimers work at the background, the code I provided works. What happens is after 10 minutes or so of being inactive, the app shuts down. To prevent this, I used Location Services instead of VoIP, since it's just easier to implement. I turned the flag on on the Info.plist file, then I made a timer that started the location services, and stopped it right away every 9 minutes. It worked like a charm ;) If you need a sample code, let me know.
    – LilDwarf
    Mar 2, 2012 at 18:01
  • Ya, please provide me code....I really need it....I am working on this more than a month...
    – Minakshi
    Mar 7, 2012 at 11:10
  • @Xyz, check out my answer for the question ;)
    – LilDwarf
    Mar 8, 2012 at 19:18

2 Answers 2

37

I figured it out by myself, for anyone else with a similar problem, what I did was to first turn on the location update flag on your Info.plist file. To do this, you must add the Key called "Required Background Modes" on Xcode 4, then as a value, select "App registers for location updates"

I have a timer declared like so in my .h file:

NSTimer *silenceTimer;
@property (nonatomic, retain) NSTimer *silenceTimer;

Then on the .m file, I declared it like so:

UIBackgroundTaskIdentifier bgTask;
UIApplication  *app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask]; 
}];
self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self
selector:@selector(startLocationServices) userInfo:nil repeats:YES];

Finally, on the selector method, I made the following:

-(void)startLocationServices {
    [locationManager startUpdatingLocation];
    [locationManager stopUpdatingLocation];
}

This will create a timer that starts and immediately stops location services after 5 minutes. This will be enough for the app to stay alive indefinately, unless you kill the process.

15
  • Thanks for your nice information.Can you tell how I can get the location update every 15 mins. I will be really thankful.
    – Cyril
    Apr 13, 2012 at 10:29
  • @Cyril, yes, you can use the same code, but instead of 300 in the timeinterval, put 900 (900 seconds = 15 min). This should execute the startLocationServices() method every 15 minutes, which activates the locationManager. For your case, i recommend removing the line [locationManager stopUpdatingLocation] and replacing it after you execute your code from the locationManagerDelegate
    – LilDwarf
    Jun 19, 2012 at 18:33
  • Wow, this is a horrific abuse of the location services exemption for background processes. I'm surprised it even works, I would have thought that stopping location services would suspend your app. In any case, this is something that should rightly get your app rejected from the store. Aug 20, 2012 at 6:51
  • 1
    money! best and most concise solution for this I have seen yet Feb 21, 2013 at 19:57
  • 1
    @Rajapandian Oh... not exactly. If it is well justified, it can get submitted. You can go ahead and submit it and if it gets rejected you can state your reasons for doing whatever you did wrong (if it involves this particular solution).
    – LilDwarf
    Mar 20, 2013 at 4:44
2

XCode 6.3.1, for iOS 8.3

There are big diffrences that I've encountered between the apps. behavior while plugged in to your Mac running XCode using debugger and the same apps. behavior while unplugged.

At current iOS version, 8.3, at most your app is allotted is 180 seconds to run in the background for finite long running task with an expiration handler.

No matter what you try, with your phone unplugged your app will be killed by the system in 180 seconds or earlier. I've done a significant amounts of tests and tricks to confirm this. See my post...

My NSTimer Post

Here is a neat way to tell how much time is left for your app to run before termination, keep in mind that you are not guaranteed this time.

NSTimeInterval backgroundTimeRemaining = [[UIApplication sharedApplication] backgroundTimeRemaining];

if (backgroundTimeRemaining == DBL_MAX)
{
    NSLog(@"background time remaining = undetermined");
}
else
{
    NSLog(@"background time remaining = %0.2f sec.", backgroundTimeRemaining);
}

Hope this helps your investigation. Cheers!

1
  • mate you are absolutely correct now we will get is 180 sec so do your work in 180 sec after that there is no way you get extra time in background , its true its working in background more then 180 sec in xcode debugger but not in production Jun 28, 2015 at 13: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.