You'll have to keep track of the background tasks you have started and make sure you don't do the work of previous tasks when a new task is started. You can do this easily by keeping an NSInteger
around in your app delegate and incrementing it for each time.
But the easier way is just this: (in place of your dispatch_async
call)
SEL methodSelector = @selector(doThisAfter30Seconds);
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:methodSelector object:nil];
[self performSelector:methodSelector withObject:nil afterDelay:30];
This will set a timer for 30 seconds and make sure the previous timers are not run. Then just implement - (void)doThisAfter30Seconds
to do whatever you want.
(You will probably want to check in doThisAfter30Seconds
that the task is still in the background, or manually remove the timer with cancelPreviousPerformRequestsWithTarget:...
in applicationWillEnterForeground:
.)