gpt4 book ai didi

IOS - 使后台获取每天只发生一次(不需要特定时间)

转载 作者:行者123 更新时间:2023-11-29 01:48:35 25 4
gpt4 key购买 nike

我正在使用后台获取来安排任务。如果我理解正确的话,后台获取在一天中会发生很多次。我需要限制它在一天中只发生一次。一切都设置正确,现在我已经设置(作为临时值)

 UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

有什么建议我可以如何实现这一点吗?

最佳答案

在您的后台提取处理程序中,检查上次执行的日期,如果那个时间是昨天,则执行新的提取并存储下一次检查的时间。确定日期是否是昨天需要一些工作:

// assumes oldDate is the last time it was actually fetched
let now = NSDate()
let cal = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)
let components = cal?.components(.CalendarUnitDay, fromDate:oldDate , toDate: now, options: .WrapComponents)
let days = components?.day
if days? > 0 {
doActualFetch()
self.oldDate = now
}

你可能会比我更想解开你的选项,但这应该会让你动起来。

您的处理程序每​​天仍会被调用多次,但您可以使用它来跳过执行实际的提取。

关于IOS - 使后台获取每天只发生一次(不需要特定时间),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31660841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com