I'm using background fetch to schedule task. If i understood correctly background fetch happens many times during the day. I need to limit it to occur only one time during the day. Everything is set up correctly,For now i have setted (as a temp value) the
UIApplication.sharedApplication().setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
Any suggestions how can i accomplish that?
In your background fetch handler, check the date it was last performed, if that time was yesterday, perform a new fetch and store the time of the next check. Figuring out if the date was yesterday takes a little work:
// 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
}
You'll probaby want to unwrap your optionals better than I did, but that should get you moving.
Your handler will still get called multiple times per day, but you can use this to skip doing the actual fetch.