Skip to content

Instantly share code, notes, and snippets.

@ratulSharker
Last active August 17, 2017 11:59
Show Gist options
  • Save ratulSharker/3ebd95a9be45995a4787d179365b45a6 to your computer and use it in GitHub Desktop.
Save ratulSharker/3ebd95a9be45995a4787d179365b45a6 to your computer and use it in GitHub Desktop.
NSTimer does not support block until the release of ios 10. This gist can serve in that situation.
#import <Foundation/Foundation.h>
//
// HISTORY...
//
// from ios 10 simillar functionality is given which is initialized
// with a block, but we need to support the older version too,
// thats why this class is been introduced
//
typedef void (^NSTimerExecutionBlock)(NSTimer*);
@interface NSTimerBlock : NSObject
-(instancetype)initWithInterval:(NSTimeInterval)interval withExecutionBlock:(NSTimerExecutionBlock)block repeats:(BOOL)repeat;
-(void)stopTimer;
@end
#import "NSTimerBlock.h"
@implementation NSTimerBlock
{
NSTimer *timer;
NSTimerExecutionBlock execBlock;
}
-(instancetype)initWithInterval:(NSTimeInterval)interval withExecutionBlock:(NSTimerExecutionBlock)block repeats:(BOOL)repeat
{
self = [super init];
if(self)
{
//
// saving the code block,
// which is to be executed later
//
execBlock = block;
//
// initiate the timer
//
timer = [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:repeat];
}
return self;
}
-(void)stopTimer
{
[timer invalidate];
timer = nil;
}
#pragma mark timer selector
-(void)timerFired:(NSTimer*)tmr
{
if(execBlock)
{
//
// some sort of execution block is
// present for processing
// so we will execute
//
execBlock(tmr);
}
else
{
[tmr invalidate];
tmr = nil;
}
}
@end
@ratulSharker
Copy link
Author

ratulSharker commented Oct 3, 2016

Do you need NSTimer with Block feature ?, ios 10 NSTimer has it. But what for the previous version ? This can serve as well the same purpose with more cleaner code.

How to use::

[[NSTimerBlock alloc] initWithInterval:2.0
                              withExecutionBlock:^(NSTimer *) {
                                 //code that you want to execute after 2.0 seconds
                            }
                                                repeats:NO];

if you want to suppress unused warning put them inside like following

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-value"
        [[NSTimerBlock alloc] initWithInterval:2.0

                            withExecutionBlock:^(NSTimer *) {

                                [[UIApplication sharedApplication] endIgnoringInteractionEvents];

                                [self dismissViewControllerAnimated:YES completion:^{
                                    [calleeView removeCalleeWithKeyNumber:nmbr];
                                }];
                            }
                                       repeats:NO];
#pragma clang diagnostic pop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment