0

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

 UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

有什么建议我怎么能做到这一点?

4

1 回答 1

1

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

// 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
}

您可能会比我更好地打开您的选项,但这应该会让您感动。

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

于 2015-07-27T19:20:50.543 回答