1
timer = [NSTimer bk_timerWithTimeInterval:60 block:^(NSTimer *timer) { ....} repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];


+ (id)bk_timerWithTimeInterval:(NSTimeInterval)inTimeInterval block:(void (^)(NSTimer *timer))block repeats:(BOOL)inRepeats
{
    NSParameterAssert(block != nil);
    return [self timerWithTimeInterval:inTimeInterval target:self selector:@selector(bk_executeBlockFromTimer:) userInfo:[block copy] repeats:inRepeats];
}

+ (void)bk_executeBlockFromTimer:(NSTimer *)aTimer {
    void (^block)(NSTimer *) = [aTimer userInfo];
    if (block) block(aTimer);
}

If I fold and unfold application after a few minutes, almost always the timer is firing immediately and sometimes it fire with delay. Why the timer does not always work immediately?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Fanruten
  • 3,782
  • 4
  • 19
  • 15

1 Answers1

-1

try to add this in "didFinishLaunchingWithOptions" in your Appdelegate

__block UIBackgroundTaskIdentifier locationUpdater =[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{

        [[UIApplication sharedApplication] endBackgroundTask:locationUpdater];

        locationUpdater=UIBackgroundTaskInvalid;

    } ];
chawki
  • 867
  • 1
  • 8
  • 13
  • Needless to say, this only gives you a finite amount of time (a few minutes) before your app is truly suspended. – Rob Mar 04 '14 at 20:37
  • I dont' want execute timer in the background. I want the timer fire immediately after the app go to the foreground. – Fanruten Mar 04 '14 at 20:41