当我们写一下代码会发现,deinit不会调用。
class ViewController: UIViewController {
var timer:Timer?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(abc), userInfo: nil, repeats: true)
}
deinit {
timer?.invalidate()
timer = nil
}
@objc func abc() {
}
}
这个因为,vc持有timer,timer也持有vc,引起循环引用。
解决以上问题可以在适当的地方解除强引用,也可以不相互持。
不相互持有可以在target中传入另一个对象。
当然这样做很麻烦,因为每次用timer的时候都必须引入另一个对象。
可以用runtime解决“另一个对象问题”
class SecViewController: UIViewController {
var timer:Timer?
var blockTimer:Timer?
override func viewDidLoad() {
super.viewDidLoad()
timer = Timer.hl_scheduledTimer(timeInterval: 1, target: self, selector: #selector(test), userInfo: nil, repeats: true)
blockTimer = Timer.hl_scheduledTimer(withTimeInterval: 1, mode: .default, repeats: true, block: {
print("blockTimer run")
})
// Do any additional setup after loading the view.
}
@objc func test() {
print("timer run")
}
deinit {
timer?.invalidate()
timer = nil
blockTimer?.invalidate()
blockTimer = nil
}
}
我封装了2个方法,它们可以像以前Timer方法那样传参,但是不会引起循环引用。可以在deinit方法正常释放timer。
hl_scheduledTimer(timeInterval:TimeInterval, target:Any, selector:Selector, userInfo:Any?, repeats:Bool)->Timer方法中引入了TimerTarget对象持有timer,在TimerTarget对象用runtime添加selector。
@discardableResult //消除返回值警告
public class func hl_scheduledTimer(timeInterval:TimeInterval, target:Any, selector:Selector, userInfo:Any?, repeats:Bool)->Timer{
let t = TimerTarget()
let imp = class_getMethodImplementation(object_getClass(target), selector)
let method = class_getInstanceMethod(object_getClass(target), selector)
let type = method_getTypeEncoding(method!)
if let _ = class_getInstanceMethod(object_getClass(t), selector){
}else{
class_addMethod(object_getClass(t), selector, imp!, type)
}
return Timer.scheduledTimer(timeInterval: timeInterval, target: t, selector: selector, userInfo: userInfo, repeats: repeats)
}
hl_scheduledTimer(withTimeInterval: TimeInterval,mode:RunLoop.Mode = .default, repeats: Bool, block: @escaping () -> Void)->Timer方法是在Timer中添加一个属性持有block,当然也引入了TimerTarget对象。
public class func hl_scheduledTimer(withTimeInterval: TimeInterval,mode:RunLoop.Mode = .default, repeats: Bool, block: @escaping () -> Void)->Timer{
let t = TimerTarget()
let timer = Timer(timeInterval: withTimeInterval, target: t, selector: #selector(TimerTarget.blockFunc(_:)), userInfo: nil, repeats: repeats)
timer.hl_timerBlock = block
RunLoop.main.add(timer, forMode: mode)
return timer
}