在特定日期使用Timer调用函数

我需要在午夜前5分钟调用函数。

仅在该时间段内用户位于包含UIView的{​​{1}}中时,才调用该函数。

我需要此收藏集查看UICollectionView 午夜前5分钟

我想到了使用reloadData()观察者

如果您想使用NotificationCenter.default.addobserver,那么在午夜前5分钟收到通知的最佳方式是什么?

您认为使用计时器代替观察者会更好吗?

其中哪些对应用程序性能更方便?

谢谢大家的帮助

hf2baobei 回答:在特定日期使用Timer调用函数

在阅读亚历山大-恢复莫妮卡的建议后,我使用为特定日期编程的计时器找到了解决问题的方法

let date = Date().addingTimeInterval(5)
let timer = Timer(fireAt: date,interval: 0,target: self,selector: #selector(runCode),userInfo: nil,repeats: false)
RunLoop.main.add(timer,forMode: .common)

此解决方案在https://www.hackingwithswift.com/example-code/system/how-to-run-code-at-a-specific-time

中有很好的解释
本文链接:https://www.f2er.com/2435250.html

大家都在问