我正在使用后台提取来安排任务。
如果我正确理解背景,一天中会发生很多次后台获取。
我需要限制它在一天中只出现一次。
一切都设置正确,现在我已经设置了(作为温度值)
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
}
您可能会想比我更好地解开选装项,但这应该会让您感动。
您的处理程序每天仍会多次被调用,但是您可以使用它来跳过实际的获取。