定时器用到项目上会有互相引用问题:直接上代码
#import@interface Person : NSObject
-(void)startTimer;
-(void)endTimer;
@end
#import "Person.h"
#import "NSTimer+HYBExtension.h"
@interface Person()
@property(nonatomic,retain)NSTimer *timer;
@end
@implementation Person
-(instancetype)init
{
self =[super init];
if (self) {
}
return self;
}
-(void)startTimer
{
__block Person * selfWeak = self;
//在这里先转成弱引用,防止定时器对person的强引用
NSTimer * timerC = [NSTimer scheduledTimerWithTimeInterval:2.0f repeats:YES callback:^{
// 换成强引用是为了调用方法时在并发时会使self变为nil
Person * StrongSelf = selfWeak;
[StrongSelf sayhello];
}];
self.timer = timerC;
//NSLog(@"ZKZTimer的引用计数:%ld",[_timer retainCount]);
}
-(void)saystr:(NSString *)ss
{
}
-(void)sayhello
{
NSLog(@"block");
//NSLog(@"---------------------------");
}
-(void)endTimer
{
[self.timer invalid];
}
-(void)dealloc
{
// NSLog(@"person dealloc");
// NSLog(@"%ld",[_Timer retainCount]);
// [_Timer removeTimer];
NSLog(@"%ld",[_timer retainCount]);
if (_timer) {
[_timer release];
_timer = nil;
}
[super dealloc];
}
创建定时器分类(分类是在网上下载下来的)
@interface NSTimer (HYBExtension)
/**
* 无参数无返回值Block
*/
typedef void (^HYBVoidBlock)(void);
/**
* 创建Timer---Block版本
*
* @param interval 每隔interval秒就回调一次callback
* @param repeats 是否重复
* @param callback 回调block
*
* @return NSTimer对象
*/
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
repeats:(BOOL)repeats
callback:(HYBVoidBlock)callback;
/**
* 创建Timer---Block版本
*
* @param interval 每隔interval秒就回调一次callback
* @param count 回调多少次后自动暂停,如果count <= 0,则表示无限次,否则表示具体的次数
* @param callback 回调block
*
* @return NSTimer对象
*/
//+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
// count:(NSInteger)count
// callback:(HYBVoidBlock)callback;
/**
* 开始启动定时器
*/
- (void)fireTimer;
/**
* Make it invalid if currently it is valid.
*/
- (void)invalid;
//
// NSTimer+HYBExtension.m
// HYBTimerExtension
//
// Created by huangyibiao on 15/4/16.
// Copyright (c) 2015年 huangyibiao. All rights reserved.
//
#import "NSTimer+HYBExtension.h"
@implementation NSTimer (HYBExtension)
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
repeats:(BOOL)repeats
callback:(HYBVoidBlock)callback {
return [NSTimer scheduledTimerWithTimeInterval:interval
target:self
selector:@selector(onTimerUpdateBlock:)
userInfo:[callback copy]
repeats:repeats];
}
//+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
// count:(NSInteger)count
// callback:(HYBVoidBlock)callback {
// if (count <= 0) {
// return [self scheduledTimerWithTimeInterval:interval repeats:YES callback:callback];
// }
//
// NSDictionary *userInfo = @{@"callback" : [callback copy],
// @"count" : @(count)};
// return [NSTimer scheduledTimerWithTimeInterval:interval
// target:self
// selector:@selector(onTimerUpdateCountBlock:)
// userInfo:userInfo
// repeats:YES];
//}
+ (void)onTimerUpdateBlock:(NSTimer *)timer {
HYBVoidBlock block = timer.userInfo;
NSLog(@"分类引用计数:%ld",[timer retainCount]);
if (block) {
block();
}
}
+ (void)onTimerUpdateCountBlock:(NSTimer *)timer {
static NSUInteger currentCount = 0;
NSDictionary *userInfo = timer.userInfo;
HYBVoidBlock callback = userInfo[@"callback"];
NSNumber *count = userInfo[@"count"];
if (currentCount < count.integerValue) {
currentCount++;
if (callback) {
callback();
}
} else {
currentCount = 0;
[timer unfireTimer];
}
}
- (void)fireTimer {
[self setFireDate:[NSDate distantPast]];
}
- (void)unfireTimer {
[self setFireDate:[NSDate distantFuture]];
}
- (void)invalid {
if (self.isValid) {
[self invalidate];
}
}
@end