首页 \ 问答 \ 每天在固定时间运行任务(Run a task at fixed time everyday)

每天在固定时间运行任务(Run a task at fixed time everyday)

我想每天在固定的时间运行一个功能(拨打api电话),比如每天上午10点和晚上10点。 什么是swift中的cronjob等价物?

我尝试将Timer实现为:

var dateComponents = DateComponents()

dateComponents.timeZone = TimeZone(abbreviation: "NPT") 
dateComponents.hour = 10
dateComponents.minute = 00

let userCalendar = Calendar.current
let myTime = userCalendar.date(from: dateComponents)

let timer = Timer(fireAt: myTime!, interval: 0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

选择器功能:

@objc func updateTimer() {
    print("Hello")
}

选择器函数不是在指定的时间运行,而是在每次运行应用程序时执行,而不是在指定的时间执行。 我怎么解决这个问题?

编辑:我也需要这个也可以从后台运行。 我将在我的应用中使用位置服务。


I would like to run a function (make an api call) at fixed time everyday, say 10 am and 10 pm daily. What would be the cronjob equivalent in swift?

I tried implementing Timer as:

var dateComponents = DateComponents()

dateComponents.timeZone = TimeZone(abbreviation: "NPT") 
dateComponents.hour = 10
dateComponents.minute = 00

let userCalendar = Calendar.current
let myTime = userCalendar.date(from: dateComponents)

let timer = Timer(fireAt: myTime!, interval: 0, target: self, selector: #selector(updateTimer), userInfo: nil, repeats: false)
RunLoop.main.add(timer, forMode: RunLoopMode.commonModes)

The selector function:

@objc func updateTimer() {
    print("Hello")
}

Instead of being run at the specified time, the selector function gets executed everytime I run the app and not at the specified time. How can I solve this?

Edit: I’ll be needing this to be run from the background as well. I’ll be using location service in my app.

更新时间:2023-02-03 21:02

最满意答案

如果不使用推送通知,就无法实现目标。 Timer正在使用运行循环,因此无法在后台运行。 也没有定期进行API调用的后台模式。

唯一的选择是每天在指定的时间从服务器发送推送通知,并响应在您的应用中收到推送通知,进行API调用。 当然,你需要互联网连接才能使推送通知正常工作,但是由于你想要进行API调用,这不会产生任何影响,因为你需要互联网连接才能使API调用成功。


There is no way to achieve your goals without using push notifications. Timer's are using run loops and hence aren't working in the background. There's no background mode for making API calls at regular intervals either.

The only option is to send out push notifications from your server at the specified times every day and in response to receiving the push notification in your app, make the API call. Of course you'll need internet connection for push notifications to work, but since you want to make API calls, this won't make a difference as you'd need internet connection for the API calls to succeed anyways.

相关问答

更多
  • 与现在的java SE 8版本一样,它具有java.time的优秀日期时间API, java.time可以更轻松地完成这些计算,而不是使用java.util.Calendar和java.util.Date 。 使用这个新API的日期时间类的LocalDateTime 使用ZonedDateTime类来处理特定于时区的计算,包括夏令时问题。 你会在这里找到教程和例子 。 现在,作为使用您的用例计划任务的示例示例: LocalDateTime localNow = LocalDateTime.n ...
  • 我的建议是使用系统调度程序(Linux上的crontab),因为它是为此目的而设计的。 如果crontab无法访问,则必须保持脚本运行,并定期检查当前时间: while True: now = datetime.datetime.now() # call the function between 23:30 and 23:35 if now.hour == 23 and 30 <= now.minute <35: sayac_yaz() time.sleep(5 ...
  • 在后台任务中添加以下内容: var task = RegisterBackgroundTask("TaskBuilder", "TimeTriggeredTask", TimeTrigger(1440, true),//1440 for the number of minutes in a day ...
  • 您没有正确引用输入,并且警报也存在语法错误。 以下是我建议的修复(工作):

    Enter Time to start dashboard

    Hour

    Minute