Swift Timer引起循环引用

当我们写一下代码会发现,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
    }

timerDemo地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值