Friday 28 November 2014

NSTimer issue in iOS8 or you are finding alternative of NSTimer


// method in every one second execute recursive "methodWhichYouWantToRecursiveCall"
[self timer];

// methodWhichYouWantToRecursiveCall calls in every 1 sec
- (void)timer {
    __weak id weakSelf = self;
    __block void (^timer)(void) = ^{
        
        double delayInSeconds = 1.0; // time for recursive call
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            
            id strongSelf = weakSelf;
            
            if (!strongSelf) {
                return;
            }
            
            // Schedule the timer again
            [self timer];
            
            // Always use strongSelf when calling a method or accessing an iVar
            [self methodWhichYouWantToRecursiveCall];
        });
    };
    
    // Start the timer for the first time
    timer();
}

No comments:

Post a Comment