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

IOS - Make background fetch Occurs only once a day (No need for specific time)

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

 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
}

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

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