Background Task scheduling and Date. Should we use absolute or calendar date?

Should we use absolute or calendar Date when scheduling background tasks?

I would guess it doesn't matter how we construct Date but I wanted to double check since WWDC videos show both ways.

I've experienced some instances where background tasks ran much later than expected, and while this is not surprising since iOS has many factors to consider when they allot background execution time, the times of execution seemed to line up with the hours change between Absolute and Calendar time.

Or

And

I understand there is no guarantee that the system will run the background task exactly or even near the date requested.

Thanks!

Answered by DTS Engineer in 773431022

The properties you’re working with here are of type Date. A Date value is a floating point offset from the epoch (the start of 2001 GMT) and thus, in your terms, it’s always absolute [1]. How you create that Date value is up to you. If you want to generate it using Calendar, that’s fine.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I don’t consider it to be absolute because it’s based on the wall clock and thus can change if the device’s time changes, but coming up with words to discuss all these concepts is hard.

Accepted Answer

The properties you’re working with here are of type Date. A Date value is a floating point offset from the epoch (the start of 2001 GMT) and thus, in your terms, it’s always absolute [1]. How you create that Date value is up to you. If you want to generate it using Calendar, that’s fine.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] I don’t consider it to be absolute because it’s based on the wall clock and thus can change if the device’s time changes, but coming up with words to discuss all these concepts is hard.

Excellent, thanks for confirming.

So I would say if you want to schedule a task for a specific time of day, like noon, Calendar can be great. And for simple time offset, like 4 hours from now, using Date().addingTimeInterval(..) seems better.

Background Task scheduling and Date. Should we use absolute or calendar date?
 
 
Q