OSX/Swift:在特定日期/时间调用功能 [英] OSX/Swift: Call function at a specific date/time

查看:48
本文介绍了OSX/Swift:在特定日期/时间调用功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让我的应用在特定时间间隔调用一个函数.例如,我可能希望每小时在每小时的该函数被调用,例如,在1:00 AM,2:00 AM等.我尝试使用NSTimer进行此操作,但是我发现在机器睡眠或关闭电源后恢复时,它难以保持同步.我的应用程序是否可以检测到何时到达特定的日期和时间并在那时调用函数?谢谢.

I'm trying to get my app to call a function at specific time intervals. For example, I might want the function to be called every hour on the hour, so at 1:00 AM, 2:00 AM, and so on. I have tried doing this with an NSTimer, but I find that it has trouble staying in sync when resuming after the machine sleeps or is powered off. Is there a way for my app to detect when we have reached a specific date and time and to call a function at that time? Thanks.

推荐答案

您可以尝试Grand Central Dispatch.具体使用 dispatch_walltime()创建一个 dispatch_time_t 来表示您希望作业运行的时间,然后使用

You could try Grand Central Dispatch. Specifically use dispatch_walltime() to create a dispatch_time_t representing the time you want the job to run and then use dispatch_after() to submit the job to Grand Central Dispatch for execution at the specified time.

这篇关于OSX/Swift:在特定日期/时间调用功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆