Andrey Chernukha Andrey Chernukha - 5 months ago 63
iOS Question

Prevent dispatch_after() background task from being executed

This is my issue. When my application enters background I want it to perform a function after certain period of time. This is what I do:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
isRunningInBackground = YES;

taskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:nil];

int64_t delayInSeconds = 30;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{
[self doSomething];
});
}

- (void)doSomething
{
NSLog(@"HELLO");
}


taskIdentifier
variable is declared in myAppDelegate.h file like this:

UIBackgroundTaskIdentifier taskIdentifier;


Everything works as it supposed to, I see that console prints HELLO just right after 30 seconds are gone. But I don't want
doSomething
to be executed if the app enters foreground until 30 seconds are over. So I need to cancel it. This is how i do that:

- (void)applicationWillEnterForeground:(UIApplication *)application
{
isRunningInBackground = NO;
[self stopBackgroundExecution];
}

- (void)stopBackgroundExecution
{
[[UIApplication sharedApplication] endBackgroundTask:taskIdentifier];
taskIdentifier = UIBackgroundTaskInvalid;
}


But unfortunately it doesn't cancel
doSomething
, it is still performed. What am I doing wrong? How do I cancel that function?

Answer

Why even use GCD? You could just use an NSTimer and invalidate it when your app returns to the foregound.