-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
让代码具有冷却时间 #15
Labels
Comments
Open
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
必要性
首先我个人认为设计合理、逻辑严谨的代码是用不到这个机制的,但是我们不能保证我们面对的代码永远都是完美的,所以我就提供了这个冷却机制以延长那些癌症晚期的代码的寿命。
优点:执行代码像放技能一样,可以强行打破死循环、避免死循环、避免过高频率访问某一资源、从一定程度上缓解了内存、CPU压力。
缺点:治标不治本,最好是找出会产生问题的代码,从根源上解决问题。
使用示例
示例1
某种耗时耗能操作,希望在某种条件下触发,但又担心用户频繁触发,就可以这样:
示例2
如何强行打破死循环?说实话我是没有遇到这种需求,仅仅是这个机制有这种能力,觉得挺有趣,就尝试一下:
实现原理
给每一个block分配一个dispatch_after的函数,执行的时候开始计时,并且函数标记为disable,计时结束后重新enable。
The text was updated successfully, but these errors were encountered: