Hello,
How should I terminate my custom implementation of TimelineSchedule?
Should I return nil or .distantFuture? Is there actual difference?
Please find my sample implementation below:
struct FixedNumberSchedule: TimelineSchedule {
let count: Int
func entries(
from startDate: Date,
mode: Mode
) -> Entries {
Entries(startDate: startDate, count: count)
}
struct Entries: Sequence, IteratorProtocol {
var currentDate: Date?
let initialCount: Int
var count: Int
init(startDate: Date,count: Int) {
self.currentDate = startDate
self.initialCount = count
self.count = count
}
mutating func next() -> Date? {
defer {
if count > 1 {
currentDate?.addTimeInterval(1)
count -= 1
} else {
self.currentDate = nil
}
}
print("Seq \(initialCount), \(currentDate, default: "none")")
return currentDate
}
}
}
Here's the catch:
For FixedNumberSchedule(count: 1) I get logs in console:
Seq 1, 2026-10-03 10:04:33 +0000
Seq 1, none
For FixedNumberSchedule(count: 2) I get infinite logs in console:
Seq 2, 2026-10-03 10:04:33 +0000
Seq 2, 2026-10-03 10:04:34 +0000
Seq 2, none
Seq 2, none
...
Side note: the displayed result is the same for both sequences (the last valid element from second sequence is never displayed).
So, If the sequence has more than a single entry to return, it actually never ends. Is this a bug?
Of course, if the sequence has ended, I could keep the code as simple as possible. But the loop still troubles me, thus my initial question:
Should I actually return nil or .distantFuture?
BR, Krystian