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
4

1 に答える 1

1

有効期限ハンドラー ブロックがタスクを終了してから再開したことが原因である可能性があります。

さらに、ブロック内でキャプチャselfしますendBackgroundTask( を使用strongSelf.backgroundTaskIdentifier)

于 2013-04-24T04:08:43.133 に答える