一、介绍
NSTimer.h文件里的一些方法、属性的讲解
1、NSInvocation创建
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation *)invocation repeats:(BOOL)yesOrNo;
需要一个NSInvocation对象创建。前者创建一个定时器,但是需要在创建定时器后手动将timer加入NSRunLoop 中;后者自动以NSDefaultRunLoopMode添加到当前线程RunLoop中。
例子:
- (void)setupTimer{
SEL sel = @selector(run);
NSMethodSignature *sign = [[self class] instanceMethodSignatureForSelector:sel];
NSInvocation *invo = [NSInvocation invocationWithMethodSignature:sign];
invo.target = self;
invo.selector = sel;
[invo invoke];
NSTimer *timer = [NSTimer timerWithTimeInterval:1 invocation:invo repeats:YES];
// NSRunLoopCommonModes NSDefaultRunLoopMode
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}
- (void)run{
NSLog(@"run");
}
2、指定target和selector
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
这两个是常用的创建方式,区别同上
3、block
+ (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block ;
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;
block创建方式,区别同上
4、实例方法
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block ;
- (instancetype)initWithFireDate:(NSDate *)date interval:(NSTimeInterval)ti target:(id)t selector:(SEL)s userInfo:(nullable id)ui repeats:(BOOL)rep ;
实例方法创建方式,区别同上
// 执行(不加入runloop,用fire执行一次)
- (void)fire;
@property (copy) NSDate *fireDate;
@property (readonly) NSTimeInterval timeInterval;
// 容差
@property NSTimeInterval tolerance API_AVAILABLE(macos(10.9), ios(7.0), watchos(2.0), tvos(9.0));
// 停止定时器再次触发并请求将其从runloop中移除
- (void)invalidate;
@property (readonly, getter=isValid) BOOL valid;
@property (nullable, readonly, retain) id userInfo;
二、实例
1、子线程开启定时器
@interface BViewController ()
@property (nonatomic,strong) NSTimer *timer;
@property (nonatomic,strong) NSThread *thread;
@end
@implementation BViewController
- (void)dealloc{
NSLog(@"注销");
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@ %@",[NSThread currentThread],[NSRunLoop currentRunLoop]);
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(perTask) object:nil];
[self.thread start];
}
- (void)perTask{
NSLog(@"%@ %@",[NSThread currentThread],[NSRunLoop currentRunLoop]);
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(run) userInfo:nil repeats:YES];
// 子线程runloop是默认不运行,必须手动开启
[[NSRunLoop currentRunLoop] run];
}
- (void)run{
NSLog(@"run里的线程%@",[NSThread currentThread]);
if ([NSThread currentThread].isCancelled) {
[self.timer invalidate];
}
NSLog(@"run");
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.thread cancel];
// if (self.thread.isCancelled) {
// [self.timer invalidate];
// }
NSLog(@"touch的线程%@",[NSThread currentThread]);
}
这个例子有几个注意点:
1、子线程runloop默认不运行,必须手动run
2、invalidate:文档上有一段说明
必须从定时器当前线程发送invalidate。 如果从另一个线程发送此消息,则与定时器关联的输入源可能不会从其runloop中删除,这可能会阻止线程正常退出。
所以销毁timer必须在子线程中即run方法里
2、循环问题
这个NStimer经典问题:当前VC持有timer,timer的target又是self(VC),从而产生循环引用。
怎么解决?
一般是按实例1中的方法,在退出VC前invalidate掉timer,但是往往我们会遗漏或者某些场景不好操作。如何优雅的解决循环引用呢?
把timer的target设为自己,这样就不会循环引用。我们用一个Category封装一下:
+ (NSTimer *)ca_scheduledTimerWithTimeInterval:(NSTimeInterval)interval
repeats:(BOOL)repeats
block:(void(^)(void))block{
return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(ca_blockInvoke:) userInfo:[block copy] repeats:repeats];
}
+ (void)ca_blockInvoke:(NSTimer *)timer{
void (^block)(void) = timer.userInfo;
if(block) {
block();
}
}
这样的话,timer事件在block中执行。
注:
1、NSTimer不是一种实时机制,这点官网文档上作了说明
A timer is not a real-time mechanism. If a timer’s firing time occurs during a long run loop callout or while the run loop is in a mode that isn't monitoring the timer, the timer doesn't fire until the next time the run loop checks the timer. Therefore, the actual time at which a timer fires can be significantly later.
2、当然了,想让NSTimer在scrollView滚动时有效需要将runloop mode换成NSRunLoopCommonModes(默认mode是NSDefaultRunLoopMode,scrollView滚动时mode切换成UITrackingRunLoopMode,所以NSTimer失效)
网友评论