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

时间:2015-07-27 18:43:05

标签: ios xcode scheduling nstimeinterval background-fetch

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

 UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)

有什么建议我能做到吗?

1 个答案:

答案 0 :(得分: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
}

你可能会比我更好地解开你的选择,但那会让你感动。

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

相关问题