Skip to content

Commit 523acd3

Browse files
committed
transfer v2.2.12 updates from LF to LF_3
1 parent 196298a commit 523acd3

2 files changed

Lines changed: 13 additions & 26 deletions

File tree

Config.xcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
unique_id = ${DEVELOPMENT_TEAM}
77

88
//Version (DEFAULT)
9-
LOOP_FOLLOW_MARKETING_VERSION = 2.2.11
9+
LOOP_FOLLOW_MARKETING_VERSION = 2.2.12

LoopFollow/Task/TaskScheduler.swift

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,10 @@ struct ScheduledTask {
2727
class TaskScheduler {
2828
static let shared = TaskScheduler()
2929

30-
// Thread-safety: a serial queue so we don’t manipulate tasks from multiple threads at once
3130
private let queue = DispatchQueue(label: "com.LoopFollow.TaskSchedulerQueue")
3231

3332
private var tasks: [TaskID: ScheduledTask] = [:]
34-
private var currentTimer: Timer?
33+
private var currentTimer: DispatchSourceTimer?
3534

3635
private init() {}
3736

@@ -52,9 +51,7 @@ class TaskScheduler {
5251
LogManager.shared.log(category: .taskScheduler, message: "Reschedule Task \(id): next run = \(timeString)", isDebug: true)
5352

5453
queue.async {
55-
guard var existingTask = self.tasks[id] else {
56-
return
57-
}
54+
guard var existingTask = self.tasks[id] else { return }
5855
existingTask.nextRun = newRunDate
5956
self.tasks[id] = existingTask
6057
self.checkTasksNow()
@@ -70,10 +67,8 @@ class TaskScheduler {
7067

7168
// MARK: - Private
7269

73-
/// Updated signature to include info about who called us, and which task triggered it (if any).
7470
private func rescheduleTimer() {
75-
// Invalidate any existing timer
76-
currentTimer?.invalidate()
71+
currentTimer?.cancel()
7772
currentTimer = nil
7873

7974
guard let (_, earliestTask) = tasks.min(by: { $0.value.nextRun < $1.value.nextRun }) else {
@@ -84,16 +79,15 @@ class TaskScheduler {
8479
let interval = earliestTask.nextRun.timeIntervalSinceNow
8580
let safeInterval = max(interval, 0)
8681

87-
// Comment out this block to simulate heartbeat execution only
88-
DispatchQueue.main.async {
89-
self.currentTimer = Timer.scheduledTimer(withTimeInterval: safeInterval, repeats: false) { [weak self] _ in
90-
guard let self = self else { return }
91-
self.queue.async {
92-
self.fireOverdueTasks()
93-
self.rescheduleTimer()
94-
}
95-
}
82+
let timer = DispatchSource.makeTimerSource(queue: self.queue)
83+
timer.schedule(deadline: .now() + safeInterval)
84+
timer.setEventHandler { [weak self] in
85+
guard let self = self else { return }
86+
self.fireOverdueTasks()
87+
self.rescheduleTimer()
9688
}
89+
currentTimer = timer
90+
timer.resume()
9791
}
9892

9993
private func fireOverdueTasks() {
@@ -107,22 +101,15 @@ class TaskScheduler {
107101
continue
108102
}
109103

110-
// Check if we should re-schedule alarmCheck till after other tasks are done
111104
if taskID == .alarmCheck {
112105
let shouldSkip = tasksToSkipAlarmCheck.contains {
113106
guard let checkTask = tasks[$0] else { return false }
114107
return checkTask.nextRun <= now || checkTask.nextRun == .distantFuture
115108
}
116-
117109
if shouldSkip {
118-
//LogManager.shared.log(category: .taskScheduler, message: "Skipping alarmCheck because one of the specified tasks is due or set to distant future.", isDebug: true)
119-
120-
guard var existingTask = self.tasks[taskID] else {
121-
continue
122-
}
110+
guard var existingTask = self.tasks[taskID] else { continue }
123111
existingTask.nextRun = Date().addingTimeInterval(5)
124112
self.tasks[taskID] = existingTask
125-
126113
continue
127114
}
128115
}

0 commit comments

Comments
 (0)