Reputation: 2217
I am working with iphone background process in my application. it is running in background but after 10 min it is terminating my application. And not working for long time.
My application contains 1 counter with timmer when viewdidload at that time my counter will start and when i will click on home button it will run in the background. In background it is running perfect but after 10 min it is stoping my background process. Following is my code for it.
in .h file.
IBOutlet UILabel *thecount;
int count;
NSTimer *theTimer;
UIBackgroundTaskIdentifier counterTask;
in my .m
- (void)viewDidLoad {
UIBackgroundTaskIdentifier bgTask = nil;
UIApplication *app = [UIApplication sharedApplication];
counterTask = [[UIApplication sharedApplication]
beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:counterTask];
//counterTask = UIBackgroundTaskInvalid;
// If you're worried about exceeding 10 minutes, handle it here
theTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(countUp)
userInfo:nil
repeats:YES];
}];
count=0;
theTimer=[NSTimer scheduledTimerWithTimeInterval:0.5
target:self
selector:@selector(countUp)
userInfo:nil
repeats:YES];
[super viewDidLoad];
}
(void)countUp {
count++;
NSString *currentCount;
currentCount=[[NSString alloc] initWithFormat:@"%d",count];
NSLog(@"timer running in background :%@",currentCount);
thecount.text=currentCount;
[currentCount release];
}
The above code just sample to count number foreground and background. but after 10 min it is not working.
So please help me and provide me some help for it.
Upvotes: 0
Views: 958
Reputation: 16193
10 minutes is the maximum you get for long-running background tasks. iOS doesn't promote the idea of long-running background processes and for the sake for your user, you shouldn't try to do them.
In order to have your code running for longer than that, some other external events may make your app be woken up again or be kept running, e.g. GPS location events, playing audio or incoming VoIP connections in your VoIP-type app.
Upvotes: 4