2013-04-24 187 views
1

我通過重複啓動計時器來嘗試長時間運行的後臺任務。但後臺任務只是第一次運行正常,下次再隨機地連續調用endBackgroundTask方法。重複長時間運行的後臺任務在第一次後臺任務後沒有啓動計時器

我的後臺任務代碼:

- (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"); 


     } 
    }); 


} 

登錄:

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 
+0

你最終得到這個工作,或者說是不可能的? – newenglander 2013-11-14 10:38:00

回答

1

這可能是由於結束任務的到期處理程序塊,然後再次啓動。

此外,你還捕捉selfendBackgroundTask塊(使用strongSelf.backgroundTaskIdentifier

+0

是的,我需要在完成第一個後臺任務後再次啓動後臺任務 – Ramprasad 2013-04-24 11:38:16

+0

當您的過期處理程序被調用時,您不應該啓動另一個任務。你應該停止你的後臺執行(清理)。 [閱讀](http://developer.apple.com/library/ios/DOCUMENTATION/UIKit/Reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler :)。 – 2013-04-24 12:25:54

+0

我需要連續運行後臺任務 – Ramprasad 2013-04-24 12:29:07