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?
Why even use GCD? You could just use an NSTimer
and invalidate it when your app returns to the foregound.
A bit different approach
OK, so, with all answers collected, and possible solutions, seems like the best one for this case (preserving simplicity) is calling performSelector:withObject:afterDelay:
and cancelling it with cancelPreviousPerformRequestsWithTarget:
call when desired. In my case - just before scheduling next delayed call:
[NSObject cancelPreviousPerformRequestsWithTarget: self selector:@selector(myDelayedMethod) object: self];
[self performSelector:@selector(myDelayedMethod) withObject: self afterDelay: desiredDelay];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With