如何在后台执行长时间运行的任务

时间:2014-04-30 11:57:59

标签: objective-c ios7

我想在Iphone的后台执行长时间运行的任务,但我用于后台任务的代码只执行3分钟

我的代码是

- (void)startService{
[self startBackgroundTask];
}

- (void) startBackgroundTask{
 self.backgroundDataSendTimer = [NSTimer scheduledTimerWithTimeInterval:backgroundDataSendfrequency
            target:self
selector:@selector(dobackgroundDataSendInBackground)
                                                                   userInfo:nil
                                                                    repeats:YES];
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
    dispatch_async(dispatch_get_main_queue(), ^
                  {
       if ( self.backgroundTask != UIBackgroundTaskInvalid)
           {
          [self endBackgroundTask];
            self.backgroundTask = UIBackgroundTaskInvalid;
            }       
  });


}];
- (void) endBackgroundTask{
[[UIApplication sharedApplication] endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid;
[self startBackgroundTask];
}

0 个答案:

没有答案