質問

I add long running task in Capabilities like 'background fetch' and add this code. Will it ok and I will have updates during all background app life?

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
    UIBackgroundTaskIdentifier bgTask = 0;
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
    }];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(backgroundUpdate) userInfo:nil repeats:YES];
    [timer fire];
}
役に立ちましたか?

解決

No it won't work.

You can't schedule a timer to repeatedly fire while the app is in the background, even if you have added a background fetch or similar capability. Adding a background mode capability DOES NOT enable your app to run freely in the background, the background modes have limitations and restrictions.

There's many many many previous questions on this topic, do some searching and read them.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top